<?php

namespace App\Http\Controllers\Supplier;

use App\Exceptions\ApiErrorException;
use App\Jobs\SendChangeOrderMessage;
use App\Models\BillReport;
use App\Models\Express;
use App\Models\MemberAlipayRisk;
use App\Models\Order;
use App\Models\OrderDepict;
use App\Models\OrderGoods;
use App\Models\OrderLease;
use App\Models\OrderLog;
use App\Models\OrderRemark;
use App\Models\OrderReturn;
use App\Models\OtherWithholdLog;
use App\Models\ShopSms;
use App\Models\ShopTrans;
use App\Models\ShopUser;
use App\Models\TransDetail;
use App\Models\OrderRiskBoolea;
use App\Models\OrderRadarBoolea;
use App\Models\OrderEduBoolea;
use App\Models\OrderCredit;
use App\Models\ShopRechargeBoolea;
use App\Models\ShopRechargeRadar;
use App\Models\ShopRechargeEdu;
use App\Models\ShopRechargeCre;
use App\Models\TwoElements;
use App\Services\AlipayMessageService;
use App\Services\AlipayRiskService;
use App\Services\AlipayService;
use App\Services\AntchainAtoService;
use App\Services\CommonService;
use App\Services\CreditCallService;
use App\Services\EsignService;
use App\Services\ExportService;
use App\Services\GoodsService;
use App\Services\LogisticsService;
use App\Services\NotarizeService;
use App\Services\OrderService;
use App\Services\RedisLock;
use App\Services\RedisService;
use App\Services\RelationVerifyService;
use App\Services\RiskService;
use App\Services\RadarService;
use App\Services\CreditService;
use App\Services\SafeService;
use App\Services\WechatService;
use Illuminate\Http\Request;
use App\Models\MemberOther;
use App\Services\EducationService;

class OrderController extends BaseController
{
    /**
     * 订单列表
     * @param Request $request
     * @return false|string
     */
    public function orderList(Request $request){
        if($this->users['role_id']=='60'||$this->users['role_id']=='62'||$this->users['role_id']=='71'||$this->users['role_id']=='72'||$this->users['role_id']=='80'){
            $request['role_id']=60;
        };
        if($this->shopUser['member_id']=='1704471738'){
            $request['seller_id']=['1704471738','1704471788'];
        }else{
            $where[] = ['o.seller_id',$this->shopUser['member_id']];
        }
        if($this->shopUser['member_id']!='1695898056'&&$this->shopUser['member_id']!='1712650315'){
            $where[] = ['o.username', '<>', 'admin'];
        }
        $shop_id=0;
        if(($this->shopUser['member_id']=='1678175782'&&$this->users['role_id']=='39')||$this->users['role_id']=='85')
        {
            $shop_id = 'tel';
            $where[] = ['o.sale_user_id','<>','022215565466'];
        }
        $data = Order::orderList($request,$where,$shop_id?$this->users['username']:'',$this->users['sale_user_id']);//
        $count = Order::computeStatus($request,$where,$shop_id?$this->users['username']:'',$this->users['sale_user_id']);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',['count'=>$count,'list'=>$data['list']]);
    }

    /**
     * 申请退押金或碎屏险
     * @param Request $request
     * @return false|string
     */
    public function applyRefundDeposit(Request $request){
        try{
            $orderId = $request->input('orderId','');
            $refund_deposit_status = $request->input('refund_deposit_status','0');
            $refund_screen_status = $request->input('refund_screen_status','0');

            if(!$orderId){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
            }
            if(!Order::checkOrderById($orderId)){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
            }
            if(empty($refund_deposit_status)&&empty($refund_screen_status)){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请选择要申请退押金或碎屏险');
            }
            $OrderDepict=OrderDepict::query()->where(['order_id'=>$orderId])->first();
            if(!$OrderDepict){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到押金信息');
            }
            if(($OrderDepict['refund_deposit_status']==1||$OrderDepict['refund_deposit_status']==2)&&$refund_deposit_status){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'该订单已申请过退押金');
            }else{
                $res= OrderDepict::query()->where(['order_id'=>$orderId])->update(['refund_deposit_status'=>1]);
            }
            if(($OrderDepict['refund_screen_status']==1||$OrderDepict['refund_screen_status']==2)&&$refund_screen_status){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'该订单已申请过退碎屏险');
            }else{
                $res=OrderDepict::query()->where(['order_id'=>$orderId])->update(['refund_screen_status'=>1]);
            }
            if($res){
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'申请成功');
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'申请失败');
            }
        }catch (\Exception $exception){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'修改失败');
        }
    }

    /**
     * 设定租赁押金
     * @param Request $request
     * @return false|string
     */
    public function setCashPledgePrice(Request $request){
        try{
            $orderId = $request->input('orderId','');
            $cash_pledge= $request->input('cash_pledge','');
            if(!$orderId || $cash_pledge<0){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
            }
            if(!Order::checkOrderById($orderId)){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
            }
            $bool=OrderDepict::query()->where('order_id',$orderId)->exists();
            if($bool){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'押金已设置过，无法修改');
            }
            $update = [
                'cash_pledge'=>$cash_pledge
            ];
            OrderGoods::where(['order_id'=>$orderId])->update($update);
            OrderLog::addOrderLog($orderId,$this->users['id'],OrderLog::ORDER_LOG_OPERATE_TYPE_ADMIN,'设置付款押金'.$cash_pledge);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'修改成功');
        }catch (\Exception $exception){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'修改失败');
        }
    }
    /**
     * 工作稳定性结果查询
     * @param Request $request
     * @return false|string
     */
    public function getStableWealthReport(Request $request){
        $order = Order::from('sp_order_2 as o')->where('o.id',$request['order_id'])
            ->leftjoin('sp_member_2 as m','m.id','o.uid')
            ->select(
                'o.id','o.uid','o.mobile as user_mobile','o.trade_no'
                ,'m.cert_name','m.cert_no','m.education'
            )
            ->first();

        if(!$order){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'订单不存在或者状态不是待付款');
        }


        if(empty($order['stable_wealth'])){
            $res=EducationService::stableWealthReport($order);
            if(isset($res['err_code'])&&$res['err_code']=='200'){
                $umem=MemberOther::where('id',$order['uid'])->update(['stable_wealth'=>isset($res['data']['stable_wealth_report'])?json_encode($res['data']['stable_wealth_report'],JSON_UNESCAPED_UNICODE):'无工作稳定性报告']);
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,$res['err_msg']);
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,$res['err_msg']);
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功');
        }
    }

    /**
     * 教育信息
     * @param Request $request
     * @return false|string
     */
    public function education(Request $request){
        $order = Order::from('sp_order_2 as o')->where('o.id',$request['order_id'])
            ->leftjoin('sp_member_2 as m','m.id','o.uid')
            ->select(
                'o.id','o.uid','o.mobile as user_mobile','o.trade_no'
                ,'m.cert_name','m.cert_no'
                ,'m.education'
            )
            ->first();

        if(!$order){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'订单不存在或者状态不是待付款');
        }
        if(empty($order['education'])){
            $res=EducationService::query($order);
            if(isset($res['retcode'])&&$res['retcode']=='000000'){
                $umem=MemberOther::where('id',$order['uid'])->update(['education'=>isset($res['data'])?json_encode($res['data']):'无学历信息']);
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,$umem?'获取成功':'获取失败');
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,$res['retmsg']);
                //   return ReturnMsg(CommonService::HTTP_CODE_FAIL,'');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',json_decode($order['education'],true));
        }
    }
    /**
     * 姓名与手机号二要素验证
     * @param Request $request
     * @return false|string
     */
    public function getAuthPhoneName(Request $request){
        if(!isset($request['phone'])||empty($request['name'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数有误');
        }
        $order = Order::from('sp_order_2 as o')->where('o.id',$request['order_id'])
            ->leftjoin('sp_member_2 as m','m.id','o.uid')
            ->select(
                'o.id','o.uid','o.mobile as user_mobile','o.trade_no'
                ,'m.cert_name','m.cert_no'
                ,'m.education'
            )
            ->first();

        if(!$order){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'订单不存在');
        }

        if(!Verify_Phone($request['phone'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '手机号码有误');
        }
        $res= TwoElements::where('order_id',$request['order_id'])
            ->where('phone',$request['phone'])
            ->value('id');
        if($res){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '此手机号码已查询');
        }

        $data=['order_id'=>$request['order_id'],'phone'=>$request['phone'],'relate'=>$request['relate'],'name'=>$request['name']];
        $res=EducationService::queryTwoName($request);
        $res=json_decode($res,true);
        if($res['code']=='0'&&$res['data']['VerificationResult']!='-1'){
            $data['result']=$res['data']['ISP'];
            if($data['result']=='CM'){
                $RelationVerifyService=new RelationVerifyService;
                $relationRes=$RelationVerifyService->query(['mobile'=>$order['mobile'],'provider'=>'1','targetNumber'=>$request['phone']]);
                $data['result_code']=$relationRes['data']['resultCode'];
                $data['vcode']=$relationRes['data']['vcode'];
            }
            $res=TwoElements::insert($data);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,$res?'获取成功':'获取失败');
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '查询失败，号码与姓名验证错误');
        }
    }
    /**
     * 姓名与手机号二要素验证
     * @param Request $request
     * @return false|string
     */
    public function getOrderCheckPhonelist(Request $request){
        $data=TwoElements::getOrderAll($request['order_id']);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data);
    }

    /**
     * 划扣订单提前提醒列表(订单划扣日提前3天),可根据某个日期筛选
     * @param Request $request
     * @return false|string
     */
    public function orderRemindList(Request $request){
        $where[] = ['o.seller_id',$this->shopUser['member_id']];
        $data = Order::orderRemindList($request,$where);
        // $count = Order::computeStatus($data['count']);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',['list'=>$data['list']]);
    }

    /**
     * 花呗订单列表
     * @param Request $request
     * @return false|string
     */
    public function orderHuaBeiList(Request $request){
        $where[] = ['o.pay_type',Order::ORDER_PAY_TYPE_HUABEI];
        $where[] = ['o.seller_id',$this->shopUser['member_id']];
        $data = Order::orderList($request,$where);
        $count = [
            OrderService::ORDER_LIST_STATUS_ALL => 0, //全部订单
            OrderService::ORDER_LIST_STATUS_NOT_PAY => 0, //待付款订单
            OrderService::ORDER_LIST_STATUS_NOT_RISK => 0, //待风控订单
            OrderService::ORDER_LIST_STATUS_DELIVERED_NOT => 0, //待发货订单
            OrderService::ORDER_LIST_STATUS_DELIVERED_IS => 0, //已发货订单
            OrderService::ORDER_LIST_STATUS_ING => 0, //租用中订单
            OrderService::ORDER_LIST_STATUS_COMPLETED => 0, //交易完成订单
            OrderService::ORDER_LIST_STATUS_CANCEL => 0, //取消订单
            OrderService::ORDER_LIST_STATUS_REFUND => 0, //退款订单
        ];
        $count = Order::computeStatus($data['count'],$count);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',['count'=>$count,'list'=>$data['list']]);
    }

    /**
     * 续租订单列表
     * @param Request $request
     * @return false|string
     */
    public function orderRenewalList(Request $request){
        $where[] = ['o.is_renewal',Order::ORDER_IS_RENEWAL];
        $where[] = ['o.seller_id',$this->shopUser['member_id']];
        $data = Order::orderList($request,$where);
        $count = [
            OrderService::ORDER_LIST_STATUS_ALL => 0, //全部订单
            OrderService::ORDER_LIST_STATUS_NOT_PAY => 0, //待付款订单
            OrderService::ORDER_LIST_STATUS_ING => 0, //租用中订单
            OrderService::ORDER_LIST_STATUS_RETURN => 0, //归还订单
            OrderService::ORDER_LIST_STATUS_COMPLETED => 0, //交易完成订单
            OrderService::ORDER_LIST_STATUS_BUY_OUT => 0, //买断订单
            OrderService::ORDER_LIST_STATUS_REFUND => 0, //退款订单
        ];
        $count = Order::computeStatus($data['count'],$count);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',['count'=>$count,'list'=>$data['list']]);
    }

    /**
     * 买断订单列表
     * @param Request $request
     * @return false|string
     */
    public function orderBuyOutList(Request $request){
        $where[] = ['o.status',OrderService::ORDER_STATUS_BUYOUT_PAY];
        $where[] = ['o.seller_id',$this->shopUser['member_id']];
        $data = Order::orderList($request,$where);
        $count = [
            OrderService::ORDER_LIST_STATUS_ALL => 0, //全部订单
        ];
        $count = Order::computeStatus($data['count'],$count);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',['count'=>$count,'list'=>$data['list']]);
    }

    /**
     * 签约待付款订单列表
     * @param Request $request
     * @return false|string
     */
    public function orderSignedList(Request $request){
        $where[] = ['o.status',OrderService::ORDER_STATUS_SIGN_NOTPAY];
        $where[] = ['o.seller_id',$this->shopUser['member_id']];
        $data = Order::orderList($request,$where);
        $count = [
            OrderService::ORDER_LIST_STATUS_ALL => 0, //全部订单
        ];
        $count = Order::computeStatus($data['count'],$count);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',['count'=>$count,'list'=>$data['list']]);
    }

    /**
     * 查看租期
     * @param Request $request
     * @return false|string
     */
    public function getLease(Request $request){

        $orderId = $request->input('orderId','');
        if(!$orderId){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        if(!Order::checkOrderById($orderId,$this->shopUser['member_id'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
        }
        $where[] = ['o.id',$orderId];
        $where[] = ['o.status', '>', OrderService::ORDER_STATUS_NOT_PAY];
        $data = Order::from('sp_order_2 as o')
            ->leftjoin('sp_order_lease_2 as ol', 'o.id','ol.order_id')
            ->leftjoin('sp_order_lease_audit as la', 'ol.id','la.lease_id')
            ->leftjoin('sp_member_2 as m', 'm.id','o.uid')
            ->where($where)
            ->select('o.trade_no','m.openId','m.session_key','ol.order_id','ol.pay_type',
                'o.status as order_status','o.month_total','ol.id',
                'ol.which_period','ol.rent','ol.begin_time',
                'ol.end_time','ol.status','ol.pay_time','ol.overdue_days','ol.overdue_amount',
                'ol.alipay_trade_no','ol.withhold_status','ol.paid_rent','la.sub_remark','la.image_path','la.check_status')
            ->orderBy('ol.which_period','asc')
            ->get();
        $data = obToArray($data);
        $dataArray=[];
        foreach ($data as $key=>$item){

            //代扣按钮 默认关闭
            $item['is_withhold'] = OrderLease::LEASE_WITHHOLD_STATUS_OFF;
            if(OrderLease::withholdLimit($item['order_status'],$item['withhold_status'],$item['begin_time'],$item['status'], true)){
                $item['is_withhold'] = OrderLease::LEASE_WITHHOLD_STATUS_NO;
            }
            $item['begin_time'] = dateDFormat($item['begin_time']);
            $item['end_time'] = dateDFormat($item['end_time']);
            $item['pay_time'] = dateFormat($item['pay_time']);
            $item['last_rent'] = bcsub($item['rent'],$item['paid_rent'],2);
            if($item['check_status']&&isset($dataArray[$item['which_period']-1])){
                $dataArray[$item['which_period']-1]=$item;
            }else{
                $dataArray[]=$item;
            }
        }

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$dataArray);
    }

    /**
     * 设置修改收货地址
     * @param Request $request
     * @return false|string
     */
    public function setAddress(Request $request){
        try{
            $orderId = $request->input('orderId','');
            $realname = $request->input('realname','');
            $mobile = $request->input('mobile','');
            $address = $request->input('address','');
            $prov = $request->input('prov','');
            $city = $request->input('city','');
            $area = $request->input('area','');
            if(!$orderId || !$realname || !$mobile || !$address || !$prov || !$city ){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
            }
            if(!Order::checkOrderById($orderId,$this->shopUser['member_id'])){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
            }
            $update = [
                'realname'=>$realname,
                'mobile'=>$mobile,
                'address'=>$address,
                'prov'=>$prov,
                'city'=>$city,
                'area'=>$area,
            ];
            Order::where(['id'=>$orderId])->update($update);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'修改成功');
        }catch (\Exception $exception){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'修改失败');
        }
    }

    /**
     * 获取租期支付流水
     * @param Request $request
     * @return false|string
     */
    public function getLeaseFlow(Request $request){
        $orderNO = $request->input('orderNo','');
        if(!$orderNO){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        if(!Order::checkOrderByNo($orderNO,$this->shopUser['member_id'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
        }
        $flow = OtherWithholdLog::getFlow($orderNO);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$flow);
    }

    /**
     * 获取签署时的人脸
     * @param Request $request
     * @return false|string
     */
    public function getSignFace(Request $request){
        try{
            $orderId = $request->input('orderId','');
            if(!$orderId){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
            }
            $order = Order::where('id',$orderId)->select('uid','face_flow_id')->first()->toArray();
            if(!$order){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
            }
//            $user= MemberOther::where('id',$order['uid'])->select('id','esign_user_id')->first()->toArray();

            $data=EsignService::selectFaceAuth($order['face_flow_id']);

            $url='';

            if(isset($data['indivInfo']['facePhotoUrl'])&&!empty($data['indivInfo']['facePhotoUrl'])){
                     $val=file_get_contents($data['indivInfo']['facePhotoUrl']);

                $url=$val;
//                     $uploud_res = AlipayService::upload($fileName,$path);
//                     if($uploud_res['status']){
//                         if(file_exists($path)){
//                             unlink($path);
//                         }
//                     }
            }
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$url);
        }catch (\Exception $exception){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'获取签署人脸照片失败');
        }
    }

    /**
     * 设置设备编号
     * @param Request $request
     * @return false|string
     */
    public function setDeviceNumber(Request $request){
        try{
            $orderId = $request->input('orderId','');
            $deviceNumber = $request->input('deviceNumber','');
            if(!$orderId || !$deviceNumber){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
            }
            if(!Order::checkOrderById($orderId,$this->shopUser['member_id'])){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
            }
            $update = [
                'device_number'=>$deviceNumber
            ];
            OrderGoods::where(['order_id'=>$orderId])->update($update);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'修改成功');
        }catch (\Exception $exception){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'修改失败');
        }
    }

    /**
     * 设置客服备注
     * @param Request $request
     * @return false|string
     */
    public function setOrderRemark(Request $request){
        try{
            $orderId = $request->input('orderId','');
            $remark = $request->input('remark','');
            if(!$orderId || !$remark){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
            }

            if(!Order::checkOrderById($orderId,$this->shopUser['member_id'])&&$this->shopUser['member_id']!='1704471738'){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
            }
            $add = [
                'uid'=>$this->users['id'],
                'username'=>$this->users['username'],
                'create_time'=>time(),
                'remark'=>$remark,
                'order_id'=>$orderId,
            ];
            OrderRemark::insert($add);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'添加成功');
        }catch (\Exception $exception){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'添加失败');
        }
    }

    /**
     * 获取客服备注
     * @param Request $request
     * @return false|string
     */
    public function getOrderRemark(Request $request){
        $orderId = $request->input('orderId','');
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',10);
        if(!$orderId){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        if(!Order::checkOrderById($orderId,$this->shopUser['member_id'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
        }
        $remark = OrderRemark::where(['order_id'=>$orderId])->select('username','create_time','remark')->orderBy('id','desc')->paginate($pageSize);
        $remark = obToArray($remark);
        if(isset($remark['data'])){
            foreach ($remark['data'] as $k=>$v){
                $remark['data'][$k]['create_time'] = dateFormat($v['create_time']);
            }
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$remark);
    }

    /**
     * 账单详情
     * @param Request $request
     * @return false|string
     */
    public function getLeaseInfo(Request $request){
        $orderId = $request->input('orderId','');
        if(!$orderId){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        if(!Order::checkOrderById($orderId,$this->shopUser['member_id'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
        }
        $leaseDetail = [];
        $order = Order::where(['id'=>$orderId])->select('month_total','pay_type')->first();
        $order = obToArray($order);
        if($order['pay_type'] == Order::ORDER_PAY_TYPE_HUABEI){
            $lease = OrderLease::where(['order_id'=>$orderId,'settle_type'=>OrderLease::ORDER_LEASE_SETTLE_STATUS_YES])
                ->whereIn('status',[OrderLease::ORDER_LEASE_STATUS_PAID,OrderLease::ORDER_LEASE_STATUS_REFUNDED,OrderLease::ORDER_LEASE_STATUS_BUYOUT])
                ->select('id','which_period','rent','trade_no','status','pay_time','pay_type','settle_type')
                ->first();
            $lease = obToArray($lease);
            if($lease){
                $lease['pay_time'] = dateFormat($lease['pay_time']);
                $trans = TransDetail::from('sp_trans_detail as d')
                    ->leftjoin('sp_shop_trans as t' ,'d.order_id','t.order_id')
                    ->select('d.*','t.amount','t.service_fee')
                    ->where(['d.order_id'=>$orderId])
                    ->orderBy('id','desc')
                    ->first();
                $trans = obToArray($trans);
                if($trans){
                    $trans['settlement_amount'] = $trans['amount'];
                    $trans['total_charge'] = $trans['service_fee'];
                }
                $lease['trans'] = $trans;
                $lease['which_period'] = "1-".$order['month_total'];
            }
            $leaseDetail[] = $lease;
        }else{
            $leaseDetail = OrderLease::where(['order_id'=>$orderId,'settle_type'=>OrderLease::ORDER_LEASE_SETTLE_STATUS_YES])
                ->whereIn('status',[OrderLease::ORDER_LEASE_STATUS_PAID,OrderLease::ORDER_LEASE_STATUS_REFUNDED,OrderLease::ORDER_LEASE_STATUS_BUYOUT])
                ->select('id','which_period','rent','trade_no','status','pay_time','pay_type','settle_type')
                ->get();
            $leaseDetail = obToArray($leaseDetail);
            foreach ($leaseDetail as &$value){
                $value['pay_time'] = dateFormat($value['pay_time']);
                $trans = TransDetail::where(['order_lease_id'=>$value['id']])->orderBy('id','desc')->first();
                $trans = obToArray($trans);
                if($trans){
                    $shopTrans  = ShopTrans::where(['order_lease_id'=>$value['id'],'trans_type'=>ShopTrans::ORDER_TRANS_TYPE_SETTLE])->select()->first();
                    $shopTrans = obToArray($shopTrans);
                    $trans['settlement_amount'] = $shopTrans ? $shopTrans['amount'] : '';
                    $trans['total_charge'] = $shopTrans ? $shopTrans['service_fee'] : '';
                }
                $value['trans'] = $trans;
            }
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$leaseDetail);
    }

    /**
     * 发货
     * @param Request $request
     * @return false|string
     */
    public function deliverGoods(Request $request){
        $orderId = $request->input('orderId','');
        $logistics_name = $request->input('logistics_name','');
        $logistics_no = $request->input('logistics_no','');
        $delivery_type = $request->input('delivery_type','');
        $images = $request->input('images','');
        if(!$orderId || !$delivery_type || !$images){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        if(!Order::checkOrderById($orderId,$this->shopUser['member_id'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
        }
        if($images){
            if(count(explode(',',$images)) > 10){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'发货凭证不能大于10张');
            }
        }


        if($delivery_type == OrderReturn::ORDER_DELIVERY_TYPE_EXPRESS){
            if(!$logistics_name || !$logistics_no){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'物流公司和单号不能为空');
            }
            if(!OrderReturn::checkOrderLogisticsNo($logistics_no)){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'快递单号已在另一个订单添加，请确认单号填写是否正确');
            }
        }

        $redis = new RedisService();
        $lock = RedisLock::lock($redis, 'orderIdRefund'.$orderId);
        if(!$lock){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '系统繁忙，请稍后再试');
        }


        $order = Order::where(['id'=>$orderId,'status'=>OrderService::ORDER_STATUS_PAY_SUCCESS])->lockForUpdate()->select('seller_id','uid','realname','trade_no','mobile','relation_id','order_from')->first();
        $order = obToArray($order);

        if(!$order){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'当前订单状态不支持发货');
        }
        $data = [
          'order_id'=>$orderId,
          'logistics_name'=>$logistics_name,
          'logistics_no'=>$logistics_no,
          'delivery_type'=>$delivery_type,
          'images'=>$images,
          'status'=>OrderReturn::ORDER_EXPRESS_STATUS_DELIVERED,
          'type'=>OrderReturn::ORDER_EXPRESS_TYPE_DELIVERY,
          'create_time'=>time(),
        ];
        $res = OrderReturn::insert($data);
        if($res){
            //更新订单状态
            Order::where(['id'=>$orderId])->update(['status'=>OrderService::ORDER_STATUS_DELIVER_SUCCESS]);
            ShopSms::orderDeliverySuccess($order['seller_id'],$order['realname'],$order['trade_no'],$order['mobile'],$logistics_name,$logistics_no);


            $where[] =['o.id', $orderId];
            $where[] =['m.openId', '<>',''];
            $where[] =['ol.out_order_no', '<>',''];
            $order = Order::from('sp_order_2 as o')
                ->leftjoin('sp_order_goods_2 as og', 'o.id', 'og.order_id')
                ->leftjoin('sp_order_lease_2 as ol', 'o.id', 'ol.order_id')
                ->leftjoin('sp_shop_user_2 as s', 'o.seller_id', 's.member_id')
                ->leftjoin('sp_order_return_2 as or', 'o.id', 'or.order_id')
                ->leftjoin('sp_goods_2 as g','og.goods_id','g.id')
                ->leftjoin('sp_member_2 as m', 'o.uid', 'm.id')
                ->select(
                    'm.openId', 'ol.out_order_no','o.id','o.trade_no','o.location','o.status','or.logistics_no','or.create_time',
                    'o.is_insurance_choose_custom','o.remark', 'o.realname', 'o.mobile as address_mobile', 'o.address', 'o.order_from'
                    , 'og.goods_title', 'og.imgurl', 'og.setmeal_title', 'og.is_buy_out', 'og.device_number', 'og.pact_type','og.setmeal_foregift','og.setmeal_price'
                    , 'm.mobile',
                )
                ->where($where)->orderBy('id','asc')->first();
            $order=obToArray($order);
            if($order&&$order['order_from']=='wechat'){
                WechatService::upload_shipping_info($order);
            }
            RedisLock::unlock($redis, 'orderIdRefund'.$orderId);

            SendChangeOrderMessage::dispatch($orderId,AlipayMessageService::KX_SHOPPING);
            SendChangeOrderMessage::dispatch($orderId,AlipayMessageService::CONSUMER)->delay(30);

            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
    }

    /**
     * 确认收货
     * @param Request $request
     * @return false|string
     */
    public function confirmGoods(Request $request){
        $orderId = $request->input('orderId','');
        $images = $request->input('images','');
        if(!$orderId || !$images){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        if(!Order::checkOrderById($orderId,$this->shopUser['member_id'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
        }
        if($images){
            if(count(explode(',',$images)) > 10){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'发货凭证不能大于10张');
            }
        }
        $order = Order::from('sp_order_2 as o')->leftjoin('sp_order_goods_2 as og','og.order_id','o.id')->where(['o.id'=>$orderId,'o.status'=>OrderService::ORDER_STATUS_DELIVER_SUCCESS])->select('o.seller_id','o.trade_no','o.realname','o.mobile','o.month_total','og.pact_type')->first();
        $order = obToArray($order);
        if(!$order){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'当前订单状态不支持确认收货');
        }

        $orderReturn  = OrderReturn::where(['order_id'=>$orderId,'type'=>OrderReturn::ORDER_EXPRESS_TYPE_DELIVERY])->where('status','>',0)->first();
        $orderReturn = obToArray($orderReturn);
        if(!$orderReturn){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'无发货信息');
        }
        if($orderReturn['delivery_type'] == OrderReturn::ORDER_DELIVERY_TYPE_EXPRESS){
            $express_data = LogisticsService::logisticsMessage($orderReturn['logistics_name'],$orderReturn['logistics_no'],$order['mobile']);
            if(isset($express_data['status']) && $express_data['status'] == 200 && $express_data['ischeck']){
                $data['info'] = json_encode($express_data);
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'此订单物流信息无法签收');
            }
        }
        $data = [
            'order_id'=>$orderId,
            'sign_proof'=>$images,
            'status'=>OrderReturn::ORDER_EXPRESS_STATUS_SIGNED,
            'confirm_time'=>time(),
        ];
        $res = OrderReturn::where(['id'=>$orderReturn['id']])->update($data);
        if($res){
            //更新订单状态
            $order_data['status'] = OrderService::ORDER_STATUS_CONFIRM_GOODS;
            if($order['pact_type'] != GoodsService::GOODS_PACT_TYPE_GIVE){
                //所有租期已全部支付，状态改为待归还
                if(OrderLease::where([['order_id',$orderId],['status',OrderLease::ORDER_LEASE_STATUS_PAID]])->count() == $order['month_total']){
                    $order_data['status'] = OrderService::ORDER_STATUS_TOBE_RETURNED;
                }
            }
            Order::where(['id'=>$orderId])->update($order_data);

            //同步订单状态
            $mobile = $order['mobile'];
            $order_arr = Order::where('mobile',$mobile)->select('order_from','old_order_id')->get();
            $order_arr = obToArray($order_arr);
            foreach($order_arr as $value){
                $order_from = $value['order_from'];
                $order_from_arr = explode('-',$order_from);
                if($order_from_arr[0] == 'tingxiangzu'&&$value['old_order_id']){
                    //发送数据
                    RadarService::setDeliverOrder($mobile,$value['old_order_id'],OrderService::ORDER_STATUS_CONFIRM_GOODS);
                }
            }

            ShopSms::orderExpressSign($order['seller_id'],$order['realname'],$order['trade_no'],$order['mobile']);
            CreditCallService::applyInvestData($orderId);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
    }

    /**
     * 补充发货确认收货凭证
     * @param Request $request
     * @return false|string
     */
    public function setOrderVoucher(Request $request){
        $orderId = $request->input('orderId','');
        $type = $request->input('type','1'); //1发货 3确认收货
        $images = $request->input('images','');
        if(!$orderId || !$type || !$images){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        if(!Order::checkOrderById($orderId,$this->shopUser['member_id'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
        }
        $orderReturn  = OrderReturn::where(['order_id'=>$orderId,'type'=>OrderReturn::ORDER_EXPRESS_TYPE_DELIVERY])->select('id','images','sign_proof','status')->first();
        $orderReturn = obToArray($orderReturn);
        if(!$orderReturn){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'无发货信息');
        }

        $update = [];
        if($type == OrderReturn::ORDER_EXPRESS_STATUS_DELIVERED){
            if(count(explode(',',$orderReturn['images'])) > 9){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'发货凭证不能大于10张');
            }
            if($orderReturn['images']){
                $images = $orderReturn['images'].','.$images;
            }
            $update = ['images'=>$images];
        }
        if($type == OrderReturn::ORDER_EXPRESS_STATUS_SIGNED){
            if($orderReturn['status'] != OrderReturn::ORDER_EXPRESS_STATUS_SIGNED){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'订单未确认收货无法上传');
            }
            if(count(explode(',',$orderReturn['sign_proof'])) > 9){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'收货凭证不能大于10张');
            }
            if($orderReturn['sign_proof']){
                $images = $orderReturn['sign_proof'].','.$images;
            }
            $update = ['sign_proof'=>$images];
        }
        if(!$update){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'凭证补充失败');
        }
        $res = OrderReturn::where(['id'=>$orderReturn['id']])->update($update);
        if($res){
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
    }

    /**
     * 获取物流信息
     * @param Request $request
     * @return false|string
     */
    public function getLogisticInfo(Request $request){
        $orderId = $request->input('orderId','');
        $type = $request->input('type','');
        if(!$orderId || !$type){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        if(!Order::checkOrderById($orderId,$this->shopUser['member_id'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
        }
        $orderReturn = OrderReturn::where(['order_id'=>$orderId,'type'=>$type])
            ->first();
        $orderReturn = obToArray($orderReturn);
        $mobile = Order::where('id',$orderId)->value('mobile');
        if($orderReturn){
            $orderReturn['create_time'] = $orderReturn['create_time'] ? dateFormat($orderReturn['create_time']) : $orderReturn['create_time'];
            $orderReturn['confirm_time'] = $orderReturn['confirm_time'] ? dateFormat($orderReturn['confirm_time']) : $orderReturn['confirm_time'];
            if ($orderReturn['status'] != OrderReturn::ORDER_EXPRESS_STATUS_SIGNED) {
                //未签收
                if (!empty($orderReturn['logistics_name']) && !empty($orderReturn['logistics_no'])) {
                    $express_data = LogisticsService::logisticsMessage($orderReturn['logistics_name'], $orderReturn['logistics_no'],$mobile);
                    if (isset($express_data['status']) && $express_data['status'] == 200 && $express_data['ischeck']) {
                        OrderReturn::where([['order_id', $orderId], ['type', $type]])
                            ->update(['info' => json_encode($express_data)]);
                    }
                    $orderReturn['info'] = $express_data;
                }
            }else{
                if(empty($orderReturn['info'])&&!empty($orderReturn['logistics_name']) && !empty($orderReturn['logistics_no'])){
                    $express_data = LogisticsService::logisticsMessage($orderReturn['logistics_name'], $orderReturn['logistics_no'],$mobile);
                    if (isset($express_data['status']) && $express_data['status'] == 200 && $express_data['ischeck']) {
                        OrderReturn::where([['order_id', $orderId], ['type', $type]])
                            ->update(['info' => json_encode($express_data)]);
                    }
                    $orderReturn['info'] = $express_data;
                }else{
                    $orderReturn['info'] = json_decode($orderReturn['info']);
                }

            }
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$orderReturn);
    }

    /**
     * 获取所有物流公司
     * @return false|string
     */
    public function getLogisticAll(){
        $express = Express::query()->pluck('name');
        $express = obToArray($express);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$express);
    }
    /**
     * 获取所有订单状态
     * @return false|string
     */
    public function getOrderStatus(){
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',OrderService::ORDER_STATUS_NAME_LIST);
    }
    /**
     * 编辑订单数据
     * @param Request $request
     * @return false|string
     */
    public function editOrderData(Request $request){

        $data = $request->input();
        $num = $data['num'];
        if($request['down_payment']>$request['setmeal_foregift']){
            return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'首付金额不能大于市场价'];
        }
        if($num == 2){
            $orderId = $request['orderId'];
            $status= Order::where('id',$orderId)
                ->value('status');
            if($status>4){
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'当前订单状态不支持修改'];
            }
            //修改租期类型
            OrderGoods::editOrderData($data);
            //修改每期租金等等
            $res = Order::editLeaseRents($data);
            //修改总租金
            if($res){
                OrderLease::editLeaseRents($data);
                //订单修改消费者提醒
                // $orderId = $data['orderId'];
                // $order = Order::from('sp_order_2')
                //         ->where('id',$orderId)
                //         ->select('realname','trade_no','mobile')
                //         ->first();
                // $order = $order ? $order->toArray() : [];
                //发送短信
                // ShopSms::orderEditSendUser($order['realname'],$order['trade_no'],$order['mobile'],$orderId);
            }
        }

        return ['status'=>CommonService::HTTP_CODE_SUCCESS,'msg'=>'修改成功'];
    }

    /**
     * 获取每期租金数据
     * @return false|string
     */
    public function getLeaseRent(Request $request){

        $orderId = $request->input('orderId','');

        $data = OrderLease::getLeaseRent($orderId);

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data);
    }
    /**
     * 获取订单编辑数据
     * @return false|string
     */
    public function getOrderData(){
        $data['pact_type'] = GoodsService::GOODS_PACT_TYPE_NAME_LIST;
        $data['month_total'] = OrderService::ORDER_MONTH_LIST;
        $data['insurance_list'] = GoodsService::GOODS_INSURANCE_LIST_SHOW;
        $data['rent_list'] = GoodsService::GOODS_RENT_LIST_SHOW;
        $data['lease_day'] = OrderService::ORDER_LEASE_DAY_LIST;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data);
    }

    /**
     * 订单列表导出
     * @param Request $request
     * @return false|string
     */
    public function orderListExport(Request $request){
        if($this->users['role_id']=='60'||$this->users['role_id']=='62'||$this->users['role_id']=='71'||$this->users['role_id']=='72'||$this->users['role_id']=='80'){
            $request['role_id']=60;
        };
        $data = ExportService::adminOrderListExport($request,$this->shopUser['member_id'],[],$this->users['sale_user_id']);
        if(!$data){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'无数据');
        }
        $params = $request->input();
        if(isset($params['orderStatus']) && $params['orderStatus']){
            $params['orderStatus'] = OrderService::ORDER_STATUS_NAME_LIST[$params['orderStatus']];
        }
        ExportService::exportFile($params,$data,ExportService::FILE_ORDER_LIST);
    }

    /**
     * 发货订单导出
     * @param Request $request
     * @return false|string
     */
    public function orderDeliverListExport(Request $request){
        if($this->users['role_id']=='60'||$this->users['role_id']=='62'||$this->users['role_id']=='71'||$this->users['role_id']=='72'||$this->users['role_id']=='80'){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'无数据');
        };
        $data = ExportService::adminOrderDeliverListExport($request,$this->shopUser['member_id'],[],$this->users['sale_user_id']);
        if(!$data){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'无数据');
        }
        $params = $request->input();
        if(isset($params['orderStatus']) && $params['orderStatus']){
            $params['orderStatus'] = OrderService::ORDER_STATUS_NAME_LIST[$params['orderStatus']];
        }
         // dd($data);
        ExportService::exportFile($params,$data,ExportService::FILE_ORDER_DELIVER_LIST);
    }

    /**
     * 划扣订单列表提前提醒表-导出
     * @param Request $request
     * @return false|string
     */
    public function orderRemindListExport(Request $request){
        $data = ExportService::adminOrderRemindListExport($request,$this->shopUser['member_id']);
        if(!$data){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'无数据');
        }
        $params = $request->input();
        if(isset($params['orderStatus']) && $params['orderStatus']){
            $params['orderStatus'] = OrderService::ORDER_STATUS_NAME_LIST[$params['orderStatus']];
        }
        ExportService::exportFile($params,$data,ExportService::FILE_ORDER_REMIND_LIST);
    }

    /**
     * 今日订单列表提前提醒表-导出
     * @param Request $request
     * @return false|string
     */
    public function orderRemindBriefListExport(Request $request){
        $data = ExportService::adminOrderRemindListBriefExport($request,$this->shopUser['member_id']);
        if(!$data){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'无数据');
        }
        $params = $request->input();
        if(isset($params['orderStatus']) && $params['orderStatus']){
            $params['orderStatus'] = OrderService::ORDER_STATUS_NAME_LIST[$params['orderStatus']];
        }
        ExportService::exportFile($params,$data,ExportService::FILE_ORDER_REMIND_BRIEF_LIST);
    }
    /**
     * 花呗订单列表导出
     * @param Request $request
     * @return false|string
     */
    public function orderHuaBeiExport(Request $request){
        $data = ExportService::adminOrderListExport($request,$this->shopUser['member_id'],[['o.pay_type',Order::ORDER_PAY_TYPE_HUABEI]]);
        if(!$data){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'无数据');
        }
        $params = $request->input();
        if(isset($params['orderStatus']) && $params['orderStatus']){
            $params['orderStatus'] = OrderService::ORDER_STATUS_NAME_LIST[$params['orderStatus']];
        }
        ExportService::exportFile($params,$data,ExportService::FILE_ORDER_LIST,ExportService::FILE_HUABEI_ORDER_LIST);
    }

    /**
     * 买断订单列表导出
     * @param Request $request
     * @return false|string
     */
    public function orderBuyOutListExport(Request $request){
        $data = ExportService::adminOrderListExport($request,$this->shopUser['member_id'],[['o.status',OrderService::ORDER_STATUS_BUYOUT_PAY]]);
        if(!$data){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'无数据');
        }
        $params = $request->input();
        if(isset($params['orderStatus']) && $params['orderStatus']){
            $params['orderStatus'] = OrderService::ORDER_STATUS_NAME_LIST[$params['orderStatus']];
        }
        ExportService::exportFile($params,$data,ExportService::FILE_ORDER_LIST,ExportService::FILE_BUYOUT_ORDER_LIST);
    }

    /**
     * 签约待支付订单列表导出
     * @param Request $request
     * @return false|string
     */
    public function orderSignedListExport(Request $request){
        $data = ExportService::adminOrderListExport($request,$this->shopUser['member_id'],[['o.status',OrderService::ORDER_STATUS_SIGN_NOTPAY]]);
        if(!$data){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'无数据');
        }
        $params = $request->input();
        if(isset($params['orderStatus']) && $params['orderStatus']){
            $params['orderStatus'] = OrderService::ORDER_STATUS_NAME_LIST[$params['orderStatus']];
        }
        ExportService::exportFile($params,$data,ExportService::FILE_ORDER_LIST,ExportService::FILE_SIGNED_ORDER_LIST);
    }

    /**
     * 待支付芝麻信用订单修改为智能合同
     * @param Request $request
     * @return false|string
     */
    public function orderContractUpdate(Request $request){
        $orderId = $request->input('orderId');
        if(!empty($orderId)){
            $order = Order::where([['id',$orderId],['status',OrderService::ORDER_STATUS_NOT_PAY]])->select('pay_type','flow_id','contract_type')->first();
            $order = obToArray($order);
            if(!empty($order)){
                if($order['pay_type'] == OrderLease::LEASE_PAY_TYPE_ANTCHAIN){
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,'已修改，请勿重复操作');
                }
                if(!empty($order['flow_id'])){
                    $orderData['flow_id'] = '';
                }
                $orderData['pay_type'] = Order::ORDER_PAY_TYPE_ANTCHAIN;
                $orderData['contract_type'] = Order::ORDER_CONTRACT_TYPE_ANTCHAIN;
                $res = Order::where('id',$orderId)->update($orderData);
                if($res){
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
                }
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'订单不存在或者状态不是待付款');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请选择要操作的订单');
        }
    }

    /**
     * 订单完结
     */
    public function orderFinish(Request $request){
        $orderId = $request->input('orderId');
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'交易完成请联系管理员');
        if(!empty($orderId)){
            $member_id = $this->users['user_id'];
            $order_res = Order::orderFinish($orderId,$member_id,true);
            if($order_res['status'] == CommonService::HTTP_CODE_SUCCESS){
                OrderLog::addOrderLog($orderId,$this->users['id'],OrderLog::ORDER_LOG_OPERATE_TYPE_ADMIN,'订单完结');
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,$order_res['msg']);
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请选择要操作的订单');
        }
    }

    /**
     * 订单同意归还
     */
    public function orderReturnAgree(Request $request){
        $orderId = $request->input('orderId');
        if(!empty($orderId)){
            if(Order::where([['id',$orderId],['status',OrderService::ORDER_STATUS_RETURN_APPLICATION]])->count() > 0){
                $res = Order::where('id',$orderId)->update(['status'=>OrderService::ORDER_STATUS_RETURN_APP_AGREE]);
                if($res){
                    OrderLog::addOrderLog($orderId,$this->users['id'],OrderLog::ORDER_LOG_OPERATE_TYPE_ADMIN,'订单同意归还');
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
                }
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'订单不存在或订单状态不是申请归还');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请选择要操作的订单');
        }
    }

    /**
     * 订单归还审核拒绝
     */
    public function orderReturnRefuse(Request $request){
        $orderId = $request->input('orderId');
        $checkRefuseReason = $request->input('checkRefuseReason');
        if(!empty($orderId) && !empty($checkRefuseReason)){
            if(Order::where([['id',$orderId],['status',OrderService::ORDER_STATUS_RETURN_APPLICATION]])->count() > 0){
                $res = Order::where('id',$orderId)->update(['status'=>OrderService::ORDER_STATUS_RETURN_APP_REFUSE,'check_refuse_reason'=>$checkRefuseReason]);
                if($res){
                    OrderLog::addOrderLog($orderId,$this->users['id'],OrderLog::ORDER_LOG_OPERATE_TYPE_ADMIN,'订单归还审核拒绝');
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
                }
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'订单不存在或订单状态不是申请归还');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'审核拒绝原因不能为空');
        }
    }


    /**
     * 订单确认归还
     */
    public function orderComfirmReturn(Request $request){
        $orderId = $request->input('orderId');
        if(!empty($orderId)){
            if(Order::where([['id',$orderId],['status',OrderService::ORDER_STATUS_RETURN_DELIVER_SUCCESS]])->count() > 0){
                $res = Order::where('id',$orderId)->update(['status'=>OrderService::ORDER_STATUS_RETURN_CONFIRM_GOODS]);
                if($res){
                    OrderLog::addOrderLog($orderId,$this->users['id'],OrderLog::ORDER_LOG_OPERATE_TYPE_ADMIN,'订单确认归还');
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
                }
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'订单不存在或订单状态不是归还用户已发货');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请选择要操作的订单');
        }
    }

    /**
     * 待支付订单关闭
     */
    public function orderClose(Request $request){
        $orderId = $request->input('orderId');
        $redis = new RedisService();
        $lock = RedisLock::lock($redis, 'orderIdRefund'.$orderId);
        if(!$lock){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '系统繁忙，请稍后再试');
        }
        if(!empty($orderId)){
            $order_res = Order::noPayOrderSupplierCancel($orderId,$this->shopUser['member_id']);
            if($order_res['status'] == CommonService::HTTP_CODE_SUCCESS){
                $add = [
                    'uid'=>$this->users['id'],
                    'username'=>$this->users['username'],
                    'create_time'=>time(),
                    'remark'=>$this->users['username'].'订单关闭',
                    'order_id'=>$orderId
                ];
                OrderRemark::insert($add);
                RedisLock::unlock($redis, 'orderIdRefund'.$orderId);
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'取消成功');
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, $order_res['msg']);
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '订单不存在');
        }
    }

    /**
     * 修改订单物流信息
     */
    public function editOrderExpress(Request $request){
        $orderId = $request->input('orderId');
        $editCode = $request->input('editCode');
        $editValue = $request->input('editValue');
        if(empty($orderId) || empty($editCode) || empty($editValue)){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        if(!Order::checkOrderById($orderId)){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
        }
        $orderReturn = OrderReturn::where('order_id',$orderId)->select('id','logistics_name','logistics_no','status')->first();
        $orderReturn = obToArray($orderReturn);
        if(!empty($orderReturn)){
            if($orderReturn[$editCode] == $editValue){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'数据未修改，请修改后再提交');
            }
            if($orderReturn['status'] == OrderReturn::ORDER_EXPRESS_STATUS_SIGNED){
                $data['status'] = OrderReturn::ORDER_EXPRESS_STATUS_TRANSIT;
            }
            $data[$editCode] = $editValue;
            $res = OrderReturn::where('id',$orderReturn['id'])->update($data);
            $log_content = '修改订单物流信息：'.$orderReturn[$editCode].'更改为'.$editValue;
            $log_res = OrderLog::addOrderLog($orderId,$this->users['id'],OrderLog::ORDER_LOG_OPERATE_TYPE_ADMIN,$log_content);
            if($res !==false && $log_res!==false){
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'修改成功');
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'修改失败，请稍后再试');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'订单发货信息不存在，请确认后再操作');
        }
    }

    /**
     * 订单雷达大数据-新颜
     */
     /*
    public function getRadarData(Request $request){
        $input = $request->input();

        if(!empty($input['orderId'])){

            $member_id = $this->shopUser['member_id'];
            //验证是否开启
            $risk_credit_status = ShopRechargeRadar::where("member_id",$member_id)->value('risk_radar_status');

            if($risk_credit_status == ShopRechargeRadar::SERVICE_STATUS_CLOSE){
                //服务关闭
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'商户未开启雷达大数据服务，无法查询结果');

            }else{

                $risk_data = OrderRadarBoolea::getMemberRiskData($input['orderId'],$member_id);
                //有则输出，无则添加
                if($risk_data){

                    $risk_data['result_detail'] = json_decode($risk_data['result'])->data->result_detail;

                    $risk_data['desc'] = json_decode($risk_data['result'])->data->desc;

                    $risk_data['result2'] = json_decode($risk_data['result2']);


                }else{
                    //查询之前减去布尔数据余量一次
                    $RechargeBoolea = ShopRechargeRadar::where(array('member_id' => $this->shopUser['member_id']))->first();

                    $RechargeBoolea = obToArray($RechargeBoolea);

                    $risk_credit_amount = $RechargeBoolea['risk_radar_amount'];

                    if($risk_credit_amount < 1){ //没有次数

                        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'雷达大数据次数不足，请购买');

                    }else{
                        ShopRechargeRadar::where(array('member_id' => $this->shopUser['member_id']))->decrement('risk_radar_amount'); //减1次

                        $risk_data = OrderRadarBoolea::getRiskData($input['orderId']); //减一次再查

                        if($risk_data){

                            $risk_data['user_type'] = OrderService::RISK_USER_TYPE_SUPPLIER; //商户查看

                            $risk_data['member_id'] = $this->shopUser['member_id']; //商户号

                            $risk_data['start_at'] = date('Y-m-d H:i:s');

                            $risk_data['end_at'] = date('Y-m-d H:i:s',time()+7*24*3600);
                            // dd($risk_data);
                            $risk_data['result_detail'] = $risk_data['data']['result_detail'];

                            unset($risk_data['id']);

                            OrderRadarBoolea::insert($risk_data);


                            $risk_data['desc'] = json_decode($risk_data['result'])->data->desc;

                            $risk_data['result2'] = json_decode($risk_data['result2']);

                        }else{
                            //全景雷达
                            $risk_data = RadarService::radarData($input);
                            // dd($risk_data);
                            OrderRadarBoolea::setRiskData($input['orderId'],$risk_data,$input['cert_name'],$input['mobile'],$input['cert_no']);

                            $risk_data['cert_name'] = $input['cert_name'];

                            $risk_data['mobile'] = $input['mobile'];

                            $risk_data['cert_no'] = $input['cert_no'];

                            $risk_data['order_id'] = $input['orderId'];


                            $risk_data['start_at'] = date('Y-m-d H:i:s',time());

                            $risk_data['end_at'] =  date('Y-m-d H:i:s',time()+7*24*60*60);

                            $risk_data['result_detail'] = $risk_data['data']['result_detail'];

                            // 探针雷达

                            $zhen_data = RadarService::radarData2($input);

                            $online_data = [];

                            $risk_data['result2'] = $online_data['result2'] = $zhen_data;

                            $online_data['user_type'] = OrderService::RISK_USER_TYPE_SUPPLIER; //管理员查看

                            $online_data['member_id'] = $this->shopUser['member_id']; //商户号
                            // var_dump($online_data);

                            OrderRadarBoolea::editRiskData($input['orderId'],$online_data);
                        }
                    }
                }

                $res = obToArray($risk_data);

                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$res);
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请选择要风控的订单');
        }
    }
    */

    /**
     * 订单雷达大数据-布尔
     */
    public function getRadarData(Request $request){
        $input = $request->input();

        if(!empty($input['orderId'])){

            $member_id = $this->shopUser['member_id'];
            //验证是否开启
            $risk_credit_status = ShopRechargeRadar::where("member_id",$member_id)->value('risk_radar_status');

            if($risk_credit_status == ShopRechargeRadar::SERVICE_STATUS_CLOSE){
                //服务关闭
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'商户未开启雷达大数据服务，无法查询结果');

            }else{

                $risk_data = OrderRadarBoolea::getMemberRiskData($input['orderId'],$member_id);
                //有则输出，无则添加
                if($risk_data){

                    $risk_data['resp_data'] = json_decode($risk_data['result'])->resp_data;
                    $risk_data['risk_probe']=json_decode($risk_data['risk_probe_result']);

                    // $risk_data['result2'] = json_decode($risk_data['result2']);

                }else{
                    //查询之前减去布尔数据余量一次
                    $RechargeBoolea = ShopRechargeRadar::where(array('member_id' => $this->shopUser['member_id']))->first();

                    $RechargeBoolea = obToArray($RechargeBoolea);

                    $risk_credit_amount = $RechargeBoolea['risk_radar_amount'];

                    if($risk_credit_amount < 1){ //没有次数

                        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'雷达大数据次数不足，请购买');

                    }else{
                        ShopRechargeRadar::where(array('member_id' => $this->shopUser['member_id']))->decrement('risk_radar_amount'); //减1次

                        $risk_data = OrderRadarBoolea::getRiskData($input['orderId']); //减一次再查

                        if($risk_data){

                            $risk_data['user_type'] = OrderService::RISK_USER_TYPE_SUPPLIER; //商户查看

                            $risk_data['member_id'] = $this->shopUser['member_id']; //商户号

                            $risk_data['start_at'] = date('Y-m-d H:i:s');

                            $risk_data['end_at'] = date('Y-m-d H:i:s',time()+7*24*3600);

                            unset($risk_data['id']);

                            OrderRadarBoolea::insert($risk_data);

                            $risk_data['resp_data'] = json_decode($risk_data['result'])->resp_data;

                            // $risk_data['result2'] = json_decode($risk_data['result2']);
                        }else{
                            //全景雷达
                            $risk_data = RiskService::radarData($input);

                            OrderRadarBoolea::setRiskData($input['orderId'],$risk_data,$input['cert_name'],$input['mobile'],$input['cert_no']);

                            $risk_data['cert_name'] = $input['cert_name'];

                            $risk_data['mobile'] = $input['mobile'];

                            $risk_data['cert_no'] = $input['cert_no'];

                            $risk_data['order_id'] = $input['orderId'];

                            $t = $risk_data['timestamp'];

                            // $times = strtotime(substr($t,0,4).'-'.substr($t,4,2).'-'.substr($t,6,2).' '.substr($t,8,2).':'.substr($t,10,2).':'.substr($t,12,2));

                            $risk_data['start_at'] = date('Y-m-d H:i:s',time());

                            $risk_data['end_at'] =  date('Y-m-d H:i:s',time()+7*24*60*60);

                            // 探针雷达

                            // $zhen_data = RiskService::zhenData($input);

                            $online_data = [];

                            // $risk_data['result2'] = $online_data['result2'] = $zhen_data['resp_code'] == 'SW0000' ? $zhen_data['resp_data'] : $zhen_data['resp_code'];

                            $online_data['user_type'] = OrderService::RISK_USER_TYPE_SUPPLIER; //管理员查看

                            $online_data['member_id'] = $this->shopUser['member_id']; //商户号
                            // var_dump($online_data);

                            OrderRadarBoolea::editRiskData($input['orderId'],$online_data);

                            $resRiskProbe=EducationService::queryRiskProbe($input);
                            OrderRadarBoolea::editRiskProbeData($input['orderId'],$resRiskProbe);
                            $risk_data['risk_probe']=json_decode($resRiskProbe,true);
                        }
                    }
                }

                $res = obToArray($risk_data);

                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$res);
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请选择要风控的订单');
        }
    }
    /**
     * 订单学历大数据
     */
    public function getEduData(Request $request){
        $input = $request->input();

        if(!empty($input['cert_no'])){

            $member_id = $this->shopUser['member_id'];
            //验证是否开启
            $risk_credit_status = ShopRechargeEdu::where("member_id",$member_id)->value('risk_edu_status');

            if($risk_credit_status == ShopRechargeEdu::SERVICE_STATUS_CLOSE){
                //服务关闭
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'商户未开启学历大数据服务，无法查询结果');

            }else{

                $risk_data = OrderEduBoolea::getMemberRiskData($input['cert_no'],$member_id);
                //有则输出，无则添加
                if($risk_data){

                    $risk_data['resp_data'] = $risk_data['result'] ? json_decode($risk_data['result'])->resp_data : $risk_data['result'];

                    $risk_data['result2'] = $risk_data['result2'] ? json_decode($risk_data['result2']) : $risk_data['result2'];

                }else{
                    //查询之前减去布尔数据余量一次
                    $RechargeBoolea = ShopRechargeEdu::where(array('member_id' => $this->shopUser['member_id']))->first();

                    $RechargeBoolea = obToArray($RechargeBoolea);

                    $risk_credit_amount = $RechargeBoolea['risk_edu_amount'];

                    if($risk_credit_amount < 1){ //没有次数

                        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'学历大数据次数不足，请购买');

                    }else{
                        ShopRechargeEdu::where(array('member_id' => $this->shopUser['member_id']))->decrement('risk_edu_amount'); //减1次

                        $risk_data = OrderEduBoolea::getRiskData($input['cert_no']);

                        if($risk_data){

                            $risk_data['user_type'] = OrderService::RISK_USER_TYPE_SUPPLIER; //商户查看

                            $risk_data['member_id'] = $this->shopUser['member_id']; //商户号

                            $risk_data['start_at'] = date('Y-m-d H:i:s');


                            unset($risk_data['id']);

                            OrderEduBoolea::insert($risk_data);

                            $risk_data['resp_data'] = $risk_data['result'] ? json_decode($risk_data['result'])->resp_data : $risk_data['result'];

                            $risk_data['result2'] = $risk_data['result2'] ? json_decode($risk_data['result2']) : $risk_data['result2'];
                        }else{

                            //学历
                            $eduNum = $input['eduNum'];

                            $risk_data = [];

                            $risk_data2 = [];

                            if($eduNum){
                                //详细学历
                                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'暂无此服务');
                                $risk_data['result2'] = [];
                                $risk_data['resp_data'] = [];
                            }else{
                                //简单学历
                                $risk_data = RiskService::eduData($input);
                                $risk_data['result2'] = [];
                            }

                            //保存
                            OrderEduBoolea::setRiskData($risk_data,$risk_data2,$input['cert_name'],$input['mobile'],$input['cert_no'],$eduNum);

                            $risk_data['cert_name'] = $input['cert_name'];

                            $risk_data['mobile'] = $input['mobile'];

                            $risk_data['cert_no'] = $input['cert_no'];

                            $risk_data['edu_num'] = $input['eduNum'];



                            $risk_data['start_at'] = date('Y-m-d H:i:s');


                            $online_data['user_type'] = OrderService::RISK_USER_TYPE_SUPPLIER; //管理员查看

                            $online_data['member_id'] = $this->shopUser['member_id']; //商户号
                            // var_dump($online_data);

                            OrderEduBoolea::editRiskData($input['cert_no'],$online_data);
                        }
                    }
                }

                $res = obToArray($risk_data);

                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$res);
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请选择要风控的订单');
        }
    }


    /**
     * 订单征信大数据
     */
    public function getCreditData(Request $request){
        $input = $request->input();

        if(!empty($input['orderId'])){

            $member_id = $this->shopUser['member_id'];

            //验证是否开启
            $risk_credit_status = ShopRechargeCre::where("member_id",$member_id)->value('risk_cre_status');

            if($risk_credit_status == ShopRechargeCre::SERVICE_STATUS_CLOSE){
                //服务关闭
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'商户未开启征信大数据服务，无法查询结果');

            }else{
                $order = Order::from('sp_order_2 as o')->where('o.id',$input['orderId'])
                    ->leftjoin('sp_member_2 as m','m.id','o.uid')
                    ->select(
                        'o.id','o.uid','o.mobile as user_mobile','o.pact_url_2','o.trade_no'
                        ,'m.cert_name','m.cert_no','m.mobile','m.esign_user_id','m.esign_seal_id','m.user_id','m.idcard_front','m.idcard_back'
                        )
                    ->first();
                $order = $order ? $order->toArray() : [];

                if(!$order['idcard_front']||  !$order['idcard_back']){
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请上传身份证照片');
                }
                //添加备注
                $add = [
                    'uid'=>$this->users['id'],
                    'username'=>$this->users['username'],
                    'create_time'=>time(),
                    'remark'=>$this->users['username'].'查看征信',
                    'order_id'=>$input['orderId']
                ];
                OrderRemark::insert($add);

                $creditData=OrderCredit::where('cert_no',$order['cert_no'])->where('result','!=','')->where('member_id',$member_id)->first();
                if($creditData){
                    Order::where('id',$order['id'])->update(['is_credit_select'=>2]);
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'从库中正在查询征信，请稍等片刻再点击按钮');
                }

                $risk_data = OrderCredit::getCreditData($input['orderId']);
                //有则输出，无则添加
                if($risk_data){
                    if($risk_data['result2']){
                        $code = json_decode($risk_data['result2'],true);
                        if($code['code'] == 'ZHZ0000'){
                            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'正在查询征信，请稍等片刻再点击按钮');
                        }else{
                            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'',['msg'=>'查询中']);
                        }
                    }else{
                        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'征信查询有误，请联系挺想租客服处理');
                    }
                }else{
                    //查询之前减去布尔数据余量一次
                    $RechargeBoolea = ShopRechargeCre::where(array('member_id' => $this->shopUser['member_id']))->first();

                    $RechargeBoolea = obToArray($RechargeBoolea);

                    $risk_credit_amount = $RechargeBoolea['risk_cre_amount'];

                    if($risk_credit_amount < 1){ //没有次数

                        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'征信大数据次数不足，请购买');

                    }else{
                        ShopRechargeCre::where(array('member_id' => $this->shopUser['member_id']))->decrement('risk_cre_amount'); //减1次

                        $risk_data = OrderCredit::getCreditData($input['orderId']); //减一次再查


                        if($risk_data){

                            $risk_data['user_type'] = OrderService::RISK_USER_TYPE_SUPPLIER; //商户查看

                            $risk_data['member_id'] = $this->shopUser['member_id']; //商户号

                            $risk_data['start_at'] = date('Y-m-d H:i:s');

                            $risk_data['end_at'] = date('Y-m-d H:i:s',time()+7*24*3600);

                            unset($risk_data['id']);

                            OrderCredit::insert($risk_data);

                            // $risk_data['data'] = json_decode($risk_data['result'])->data;


                        }else{
                            //征信
//                            $risk_data = CreditService::getInvestData($order);
//
//                           if($risk_data['code'] == 0){
//                                OrderCredit::setRiskData($input['orderId'],$risk_data,$order['cert_name'],$order['mobile'],$order['cert_no']);
//                             }else{
//                               return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请求查看失败，请稍后再尝试');
//
//                           }

                            if(empty($order['pact_url_2'])||empty($order['pact_url_3'])){
                                //未签署个人征信授权书
                                $countResOne=OrderCredit::where('order_id',$order['id'])->lockForUpdate()->first();
                                if(!$countResOne){
                                    $risk_data=['code'=>'0','message'=>'查询中'];
                                    OrderCredit::setRiskData($input['orderId'],$risk_data,$order['cert_name'],$order['mobile'],$order['cert_no']);
                                    OrderService::createTwo($order);
                                    OrderService::createTwoOne($order);
                                }
                            }

                            $risk_data=['code'=>'0','message'=>'查询中'];
                            OrderCredit::setRiskData($input['orderId'],$risk_data,$order['cert_name'],$order['mobile'],$order['cert_no']);

                            $online_data['user_type'] = OrderService::RISK_USER_TYPE_SUPPLIER; //商户查看

                            $online_data['member_id'] = $this->shopUser['member_id']; //商户号


                            $online_data['username'] = $this->users['username']; //查看人员
                            // var_dump($online_data);


                            OrderCredit::editCreditData($input['orderId'],$online_data);
                        }
                    }
                }

                // $res = obToArray($risk_data);

                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'请求成功');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请选择要征信风控的订单');
        }
    }
    /**
     * 订单征信-个人征信授权书-查询订单创建-新征信接口
     */
    public function getCreditData2(Request $request){

        $input = $request->input();
        if(!empty($input['orderId'])){
            //查询
            $order = Order::from('sp_order_2 as o')->where('o.id',$input['orderId'])
                ->leftjoin('sp_member_2 as m','m.id','o.uid')
                ->select(
                    'o.id','o.uid','o.mobile as user_mobile','o.pact_url_2','o.trade_no'
                    ,'m.cert_name','m.cert_no'
                    ,'m.mobile','m.esign_user_id','m.esign_seal_id','m.user_id','m.idcard_front','m.idcard_back'
                )
                ->first();
            $order = $order ? $order->toArray() : [];
            $risk_data=OrderCredit::where('cert_no',$order['cert_no'])->where('result','!=','')->where('start_at','>',date('Y-m-d H:i:s',strtotime('-7 day')))->orderby('id','desc')->first();
            if(!$risk_data){
                $risk_data=OrderCredit::getCreditData($order['id']);
            }
            $risk_data_res=$risk_data;
            $risk_data2 = json_decode($risk_data['result2'],true);
            //有数据
            if($risk_data){
                $result = json_decode($risk_data['result2'],true);
                if($result){//有征信结果，输出
                    if(!isset($result['code'])){
                        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'没有查到征信');
                    }
                    $code1 =$result['code'];
                    // dd($code1);
                    if($code1 != 'ZHZ0000'){
                        return ReturnMsg(CommonService::HTTP_CODE_FAIL,$result['message']);
                    }else{
                        $resa= json_decode($risk_data['result'],true);

                        if($resa['code'] == 'ZHZ0000' ){
                            $risk_data['data'] = json_decode($risk_data['result'])->data;
                        }else if(!isset($resa['code']) ){
                            $risk_data = CreditCallService::getInvestData2($risk_data['requestId'],$risk_data['scoreNo']);
                            $online_data['result'] = json_encode($risk_data);

                            $risk_data['cert_name'] = $risk_data_res['cert_name'];

                            $risk_data['mobile'] = $risk_data_res['mobile'];

                            $risk_data['cert_no'] = $risk_data_res['cert_no'];

                            $risk_data['order_id'] = $input['orderId'];


                            $risk_data['start_at'] = date('Y-m-d H:i:s',time());

                            $risk_data['end_at'] =  date('Y-m-d H:i:s',time()+7*24*60*60);

                            $online_data['result'] = json_encode($risk_data);

                            OrderCredit::editCreditData($input['orderId'],$online_data);
                            if($risk_data['code'] != 'ZHZ0000'){
                                return ReturnMsg(CommonService::HTTP_CODE_FAIL,$risk_data['message']);
                            }
                            $risk_data['result']=$online_data['result'];
                        }else if(isset($resa['code']) ){
                            $risk_data = CreditCallService::getInvestData2($risk_data['requestId'],$risk_data['scoreNo']);
                            $online_data['result'] = json_encode($risk_data);

                            $risk_data['cert_name'] = $risk_data_res['cert_name'];

                            $risk_data['mobile'] = $risk_data_res['mobile'];

                            $risk_data['cert_no'] = $risk_data_res['cert_no'];

                            $risk_data['order_id'] = $input['orderId'];


                            $risk_data['start_at'] = date('Y-m-d H:i:s',time());

                            $risk_data['end_at'] =  date('Y-m-d H:i:s',time()+7*24*60*60);

                            $online_data['result'] = json_encode($risk_data);

                            OrderCredit::editCreditData($input['orderId'],$online_data);
                            if($risk_data['code'] != 'ZHZ0000'){
                                return ReturnMsg(CommonService::HTTP_CODE_FAIL,$risk_data['message']);
                            }
                            $risk_data['result']=$online_data['result'];
                        }

                        //结果已出来
                        $risk_data_res['data'] = json_decode($risk_data['result'],true)['data'];
                    }
                }
                $res = obToArray($risk_data);
                if(!$res['data']){
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,$res['data']['result']['message']);
                }

                $jsonString = str_replace("'", '"', $res['data']['result']['modelResult']);
                $res['data']['result']['modelResult'] = json_decode($jsonString, true);
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'请求成功',$res);

            }else{ //没有数据
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请查询后再获取');
            }

        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请选择要查询征信报告的订单');
        }
    }

    /**
     * 订单风控大数据
     */
    public function getRiskData(Request $request){
        $input = $request->input();

        if(!empty($input['orderId'])){

            $member_id = $this->shopUser['member_id'];
            //验证是否开启
            $risk_credit_status = ShopRechargeBoolea::where("member_id",$member_id)->value('risk_credit_status');

            if($risk_credit_status == ShopRechargeBoolea::SERVICE_STATUS_CLOSE){
                //服务关闭
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'商户未开启布尔风控大数据服务，无法查询结果');

            }else{

                $risk_data = OrderRiskBoolea::getMemberRiskData($input['orderId'],$member_id);
                //有则输出，无则添加
                if($risk_data){

                    $risk_data['result'] = json_decode($risk_data['result']);

                    $risk_data['resp_data'] = $risk_data['result']->resp_data;

                }else{
                    //查询之前减去布尔数据余量一次
                    $RechargeBoolea = ShopRechargeBoolea::where(array('member_id' => $this->shopUser['member_id']))->first();

                    $RechargeBoolea = obToArray($RechargeBoolea);

                    $risk_credit_amount = $RechargeBoolea['risk_credit_amount'];

                    if($risk_credit_amount < 1){ //没有次数

                        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'布尔数据次数不足，请购买');

                    }else{
                        ShopRechargeBoolea::where(array('member_id' => $this->shopUser['member_id']))->decrement('risk_credit_amount'); //减1次

                        $risk_data = OrderRiskBoolea::getRiskData($input['orderId']); //减一次再查

                        if($risk_data){


                            $risk_data['user_type'] = OrderService::RISK_USER_TYPE_SUPPLIER; //商户查看

                            $risk_data['member_id'] = $this->shopUser['member_id']; //商户号

                            $risk_data['start_at'] = date('Y-m-d H:i:s');

                            $risk_data['end_at'] = date('Y-m-d H:i:s',time()+7*24*3600);

                            unset($risk_data['id']);

                            OrderRiskBoolea::insert($risk_data);

                            $risk_data['result'] = json_decode($risk_data['result']);

                            $risk_data['resp_data'] = $risk_data['result']->resp_data;


                        }else{

                            $risk_data = RiskService::booleData($input);

                            OrderRiskBoolea::setRiskData($input['orderId'],$risk_data);

                            $risk_data['order_id'] = $input['orderId'];

                            $t = $risk_data['timestamp'];


                            $risk_data['start_at'] = date('Y-m-d H:i:s',time());

                            $risk_data['end_at'] =  date('Y-m-d H:i:s',time()+7*24*60*60);



                            $online_data = [];


                            $online_data['user_type'] = OrderService::RISK_USER_TYPE_SUPPLIER; //商户查看

                            $online_data['member_id'] = $this->shopUser['member_id']; //商户号

                            OrderRiskBoolea::editRiskData($input['orderId'],$online_data);

                        }
                    }
                }


//                var_dump($risk_data);

                $res = obToArray($risk_data);

                $online = [
                    '1'=>'0-3个月',
                    '2'=>'3-6个月',
                    '3'=>'6个月-1年',
                    '4'=>'1年-2年',
                    '5'=>'2年以上',
                    '0'=>'查无记录',
                    '-1'=>'不支持该运营商',
                    '999'=>'手机状态异常（注:1、销号6月以上；2、携号转网；3、未放出去的号码）或者非本网手机号码',
                    '-999'=>'手机状态异常（注:1、销号6月以上；2、携号转网；3、未放出去的号码）或者非本网手机号码',
                ];

                $goods_type = [
                    '0'=>'其他',
                    '1'=>'游戏电玩',
                    '2'=>'娱乐影⾳',
                    '3'=>'电脑平板',
                    '4'=>'⼿机优选'
                ];

                $new_old = [
                    '0'=>'全新',
                    '1'=>'二手'
                ];

                $res['resp_data']['ident_info_dict']['time_online'] = $online[$res['resp_data']['ident_info_dict']['time_online']];

                $res['resp_data']['base_info']['goods_type'] = $goods_type[$res['resp_data']['base_info']['goods_type']];

                $res['resp_data']['base_info']['status'] = $new_old[$res['resp_data']['base_info']['status']];


                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$res);
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请选择要风控的订单');
        }
    }

    /**
     * 订单审核通过
     */
    public function orderCheckPass(Request $request){
        $orderId = $request->input('orderId');
        // dd($orderId);
        if(!empty($orderId)){
            if(Order::where([['id',$orderId],['status',OrderService::ORDER_STATUS_SIGN_NOCHECK]])->count() > 0){
                //订单征信-上征信-担保放款
                $order = Order::from('sp_order_2 as o')->where('o.id',$orderId)
                    ->leftjoin('sp_order_credit as c','c.order_id','o.id')
                    ->leftjoin('sp_member_2 as u','o.uid','u.id')
                    ->leftjoin('sp_shop_user_2 as su','o.seller_id','su.member_id')
                    ->leftjoin('sp_order_goods_2 as og','o.id','og.order_id')
                    ->leftjoin('sp_goods_2 as g','og.goods_id','g.id')
                    ->select(
                        'o.id','o.trade_no','o.begin_time','o.zulinbao_order_id','o.end_time','o.pact_url_3','o.uid','o.seller_id','o.is_insurance_choose','o.is_insurance_choose_custom',
                        'o.flow_id','o.mobile','o.user_email','o.sign_num','o.address','o.rent_total','o.month_total','o.amount','o.realname','o.rent_total',
                        'o.foregift','o.status','o.prov','o.city','o.area','o.sign_status','o.is_rent_choose'
                        ,'c.platformLoanNo','o.create_time','og.lease_day'
                        ,'u.ant_user_id','u.mobile as user_mobile','u.id as x_user_id','u.cert_name','u.user_id','u.cert_no','u.esign_user_id'
                        ,'u.idcard_front','u.idcard_back','og.pact_type','og.is_buy_out','og.goods_title','og.setmeal_title','og.lease_set','og.setmeal_price','og.setmeal_foregift',
                        'og.goods_id','og.setmeal_id','og.id as order_goods_id','og.spec_id','og.lease_price','og.lease_time','og.website_price','og.start_date'
                        ,'su.mechanism','su.faname','su.shopname','su.contact_tel','su.company','su.company_area','su.companyid','su.id as shop_user_id'
                        ,'su.ant_user_id as mer_ant_id','u.openId'
                        ,'g.new_grade')
                    ->first();
                $order = $order ? $order->toArray() : [];

//                if(mb_strlen($address)>35){
//                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,'禁用');
//                }
                if(!strstr($order['address'],'省')&&!strstr($order['address'],'市')){
                    $address=$order['prov'].$order['city'].$order['area'].$order['address'];
                }else{
                    $address=$order['address'];
                }
                if(mb_strlen($address)>35){
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,'用户收货地址过长，请手动修改下');
                }

                if(empty($order['openId'])){
                    //获取合同签署地址
                    $res = AntchainAtoService::syncBehindOrder($order);
                    if ($res && $res['resultCode'] != 'OK') {
                        return ReturnMsg(CommonService::HTTP_CODE_FAIL, $res['resultMsg']);
                    }
//                    $res = OrderService::getSignUrl($order);
//                    $res = json_decode($res,true);
//
//                    if($res && $res['status'] == 100){
//                        return ReturnMsg(CommonService::HTTP_CODE_FAIL,$res['data']['message']);
//                    }
                }


                Order::where('id',$orderId)->update(['status'=>OrderService::ORDER_STATUS_SIGN_NOTPAY]);

//                if($order['pact_url_3']){
//                    //放款计划
//                    $order_lease = OrderLease::where('order_id',$orderId)
//                                    ->select(
//                                        'rent as needRepayAmount'
//                                        ,'rent as needRepayCapital'
//                                        ,'which_period as period'
//                                        ,'begin_time')
//                                    ->get();
//                    $order_lease = $order_lease ? $order_lease->toArray() : [];
//
//                    foreach($order_lease as $key=>$value){
//                        $order_lease[$key]['repayDate'] = date('Y-m-d H:i:s',$value['begin_time']);
//                    }
//
//
//                    $credit = OrderCredit::remitInvestData($order,$order_lease);
//
//                    if($credit['code'] == 0){
//                        $update_data = [
//                            'status1'=>'1',
//                            'status_time'=>date('Y-m-d H:i:s'),
//                            'result3'=>json_encode($credit)
//                        ];
//                        OrderCredit::editCreditData($orderId,$update_data);
//                    }else{
//                        $update_data = [
//                            'result3'=>json_encode($credit)
//                        ];
//                        OrderCredit::editCreditData($orderId,$update_data);
//
//                       return ReturnMsg(CommonService::HTTP_CODE_FAIL,'征信放款失败,请查看原因');
//                    }
//                }


                OrderLog::addOrderLog($orderId,$this->users['id'],OrderLog::ORDER_LOG_OPERATE_TYPE_ADMIN,'订单审核通过');

                $contract_no=MemberOther::where('id',$order['uid'])->value('contract_no');
                if($contract_no){
                    $url = 'http://tracking.e.qq.com/conv';
                    $param = [
                        'actions'=>[
                            [
                                "action_time"=>time(),
                                "user_id"=>['wechat_openid'=>$order['openId'],'wechat_app_id'=>'wx19d3c0ddc53497ea'],
                                "action_type"=>"COMPLETE_ORDER",
                                "trace"=>['click_id'=>$contract_no],
                                "action_param"=>['value'=>'0'],
                            ]
                        ],
                    ];
                    $result = http_post($url,json_encode($param),['Content-type: application/json;charset=UTF-8']);
                    $result=json_decode($result,true);
                    $content = [
                        'request'=>is_object($param) ? object_array($param) : $param,
                        'response'=>is_object($result) ? object_array($result) : $result,
                    ];
                    \Illuminate\Support\Facades\Log::debug('下单转化归因',$content);
                }
                SendChangeOrderMessage::dispatch($orderId,AlipayMessageService::KX_SHOPPING);
                SendChangeOrderMessage::dispatch($orderId,AlipayMessageService::CONSUMER)->delay(30);

                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');

            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'订单不存在或订单状态不是待审核');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'缺少参数');
        }
    }

    /**
     * 订单转回平台
     */
    public function returnOrder(Request $request){
        $orderId = $request->input('orderId');
        if(!empty($orderId)){
            OrderLog::addOrderLog($orderId,$this->users['id'],OrderLog::ORDER_LOG_OPERATE_TYPE_ADMIN,'订单转回平台');
            $data = array(
                'orderId'=>$orderId,
                'seller_id'=>'1677219996'
            );
            Order::editOrderData($data,$this->users['username']);


            //添加备注说明

            $add = [
                'uid'=>$this->users['id'],
                'username'=>'平台方', //只有总后台能看到
                'create_time'=>time(),
                'remark'=>$this->shopUser['shopname'].'已退回给平台',
                'order_id'=>$orderId,
            ];
            OrderRemark::insert($add);

            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'缺少参数');
        }
    }

    /**
     * 设定拿货价
     * @param Request $request
     * @return false|string
     */
    public function setAcquisitionPrice(Request $request){
        try{
            $orderId = $request->input('orderId','');
            $acquisitionPrice = $request->input('acquisition_price','');
            if(!$orderId || $acquisitionPrice<0){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
            }
            if(!Order::checkOrderById($orderId)){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'未找到订单信息');
            }
            $update = [
                'acquisition_price'=>$acquisitionPrice
            ];
            OrderGoods::where(['order_id'=>$orderId])->update($update);
            OrderLog::addOrderLog($orderId,$this->users['id'],OrderLog::ORDER_LOG_OPERATE_TYPE_ADMIN,'设置拿货价'.$acquisitionPrice);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'修改成功');
        }catch (\Exception $exception){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'修改失败');
        }
    }



    /**
     * 订单征信-上征信-签署用户授权协议
     */
    public function setCreditData(Request $request){
        $input = $request->input();

        if(!empty($input['orderId'])){
            //订单征信-上征信-签署用户授权协议
            $order = Order::from('sp_order_2 as o')->where('o.id',$input['orderId'])
                ->leftjoin('sp_member_2 as m','m.id','o.uid')
                ->select(
                    'o.id','o.uid','o.mobile as user_mobile','o.pact_url_2','o.pact_url_3'
                    ,'m.cert_name','m.cert_no','m.mobile','m.esign_user_id','m.esign_seal_id','m.user_id','m.idcard_front','m.idcard_back'
                    )
                ->first();
            $order = $order ? $order->toArray() : [];
            if(empty($order['pact_url_3'])){
                $res = OrderService::createOne($order);
                if($res){
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
                }
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'已上征信，请勿重复操作');
            }


        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请选择要上征信报告的订单');
        }
    }
    /**
     * 订单征信-上征信-申请担保
     */
    public function setCreditData2(Request $request){
        $input = $request->input();

        if(!empty($input['orderId'])){
            //订单征信-上征信-签署用户授权协议
            $order = Order::from('sp_order_2 as o')->where('o.id',$input['orderId'])
                ->leftjoin('sp_member_2 as m','m.id','o.uid')
                ->select(
                    'o.id','o.pact_url_2','o.pact_url_3','o.is_credit_up','o.address','o.trade_no','o.rent_total','o.month_total','o.begin_time','o.end_time','o.city'
                    ,'m.cert_name','m.cert_no','m.mobile','m.idcard_front','m.idcard_back','m.gender'
                    )
                ->first();
            $order = $order ? $order->toArray() : [];
            if($order['is_credit_up'] == '1'){

                $order['rid'] = Areas::where('name',$order['city'])->value('rid');

                $res = CreditService::applyInvestData($order,$input);
                if($res['code'] == '0'){

                    Order::where('id',$input['orderId'])->update(['is_credit_up'=>2]);

                    //更新订单征信数据
                    $online_data = [];
                    $online_data['platformLoanNo'] = $res['data']['platformLoanNo'];
                    $online_data['loanNo'] = $order['trade_no'];
                    $online_data['is_set'] = '1';

                    OrderCredit::editCreditData($input['orderId'],$online_data);

                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
                }
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'已上征信，请勿重复操作');
            }



        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请选择要上征信报告的订单');
        }
    }

    /**
     * 更新身份证证件
     * @param Request $request
     * @return false|string
     */
    public function updateMemberCard(Request $request){
        if(empty($request['order_id'])){
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'请选择订单');
        }
        $member_id=order::where('id',$request['order_id'])->value('uid');
        $res=MemberOther::where('id',$member_id)->update(['idcard_front'=>$request['front'],'idcard_back'=>$request['back']]);
        if($res!==false){
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'修改成功');
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'修改失败');

        }
    }
    /**
     * 更新订单设备锁
     * @param Request $request
     * @return false|string
     */
    public function updateOrderLock(Request $request){
        if(empty($request['order_id'])){
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'请选择订单');
        }
        $order=order::where('id',$request['order_id'])->select('id','is_lock')
            ->first();
        $order = $order ? $order->toArray() : [];
        if(empty($order)){
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'请选择订单');
        }
        $res=Order::where('id',$order['id'])->update(['is_lock'=>$order['is_lock']?0:1]);
        if($res){
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'修改成功');
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'修改失败');

        }
    }

    /**
     * 订单详情
     * @param Request $request
     * @return false|string
     */
    public function orderDetail(Request $request){

        $orderId = Order::where('is_handle','2')
                ->where('status',OrderService::ORDER_STATUS_SIGN_NOCHECK)
                ->where('username',$this->users['username'])
                ->where('seller_id',$this->shopUser['member_id'])
                ->value('id');
        //1678175782  盈都  1685775283 小白
         if(!$orderId && $this->shopUser['member_id']=='1678175782'){
            $orderId = Order::where('is_handle','2')
                ->where('status',OrderService::ORDER_STATUS_SIGN_NOCHECK)
                ->where('username','tingxiangzu')
                ->where('seller_id',$this->shopUser['member_id'])
                ->whereIn('sale_user_id',['012418193027','012418205971','012610455071','012610463751','042419335455'])
                ->orderBy('create_time','DESC')
                ->value('id');
        }
        if(!$orderId && $this->shopUser['member_id']=='1685775283'){
            $orderId = Order::where('is_handle','2')
                ->where('status',OrderService::ORDER_STATUS_SIGN_NOCHECK)
                ->where('username','tingxiangzu')
                ->where('seller_id',$this->shopUser['member_id'])
                ->orderBy('create_time','ASC')
                ->value('id');
        }

        if(!$orderId && $this->shopUser['member_id']=='1691726298'){
            $orderId = Order::where('is_handle','2')
                ->where('status',OrderService::ORDER_STATUS_SIGN_NOCHECK)
                ->where('username','tingxiangzu')
                ->where('seller_id',$this->shopUser['member_id'])
                ->orderBy('create_time','ASC')
                ->value('id');
        }

        if(!$orderId && $this->shopUser['member_id']=='1687342445'){
            $orderId = Order::where('is_handle','2')
                ->where('status',OrderService::ORDER_STATUS_SIGN_NOCHECK)
                ->where('username','tingxiangzu')
                ->where('seller_id',$this->shopUser['member_id'])
                ->orderBy('create_time','ASC')
                ->value('id');
        }

        if(!$orderId && $this->shopUser['member_id']=='1704471738'){
            $orderId = Order::where('is_handle','2')
                ->where('status',OrderService::ORDER_STATUS_SIGN_NOCHECK)
                ->where('username','tingxiangzu')
                ->where('seller_id',$this->shopUser['member_id'])
                ->orderBy('create_time','DESC')
                ->value('id');
        }
        // dd($orderId);


        if($orderId){
            Order::where('id',$orderId)->update(['username'=>$this->users['username']]);
            //添加备注说明
            $remark = OrderRemark::where('uid',$this->users['id'])
                    ->where('order_id',$orderId)
                    ->where('remark','订单审核人员：'.$this->users['username'])
                    ->where('username','平台方')
                    ->first();
            $remark = obToArray($remark);
            if(empty($remark)){
                $add = [
                    'uid'=>$this->users['id'],
                    'username'=>'平台方', //只有总后台能看到
                    'create_time'=>time(),
                    'remark'=>'订单审核人员：'.$this->users['username'],
                    'order_id'=>$orderId,
                ];
                OrderRemark::insert($add);
            }
            $data = Order::orderDetail($orderId,$this->shopUser['member_id'],$this->users['username']);

            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data);
        }else{

            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'暂无审核中订单');
        }

    }

    /**
     * 芝麻风控
     * @param Request $request
     * @return false|string
     */
    public function rentRiskCreditQuery(Request $request){
        $order= Order::from('sp_order_2 as o')
            ->leftjoin('sp_member_2 as m', 'o.uid', 'm.id')
            ->leftjoin('sp_order_goods_2 as og', 'o.id', 'og.order_id')
            ->leftjoin('sp_shop_user_2 as s', 'o.seller_id', 's.member_id')
            ->where('o.id',$request['order_id'])
            ->select('m.mobile', 'm.cert_name', 's.mechanism', 'm.cert_no', 'o.mini_id', 'o.trade_no', 'o.realname', 'o.address',
                'o.month_total', 'o.address', 'o.address', 'o.id', 's.member_id', 'o.uid', 'o.alipay_trade_no', 'o.rights_auth_no', 'o.seller_id', 'og.goods_title', 'og.imgurl', 'm.gender', 'og.setmeal_title')
            ->first()->toArray();

        if(empty($order['alipay_trade_no'])||empty($order['rights_auth_no'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'用户未授权芝麻免押');
        }
        $res = AlipayService::alipayCreditPreQuery($order['mini_id'], $order);
        if(!$res['status']){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,$res['message']);
        }
        //添加备注
        $add = [
            'uid'=>$this->users['id'],
            'username'=>$this->users['username'],
            'create_time'=>time(),
            'remark'=>$this->users['username'].'查询芝麻风控',
            'order_id'=>$request['order_id']
        ];
        OrderRemark::insert($add);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功');
    }

    /**
     * 小程序云智能租赁风控查询
     * @param Request $request
     * @return false|string
     */
    public function rentRiskQuery(Request $request){
        $order = Order::from('sp_order_2 as o')->where('o.id', $request['order_id'])
            ->select(
                'o.id', 'o.uid', 'o.trade_no'
            )
            ->first();
        $one = MemberAlipayRisk::query($order['uid']);
        if (!empty($one)) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '已查询');
        }
        $order = Order::from('sp_order_2 as o')
            ->leftjoin('sp_member_2 as m', 'o.uid', 'm.id')
            ->leftjoin('sp_order_goods_2 as og', 'o.id', 'og.order_id')
            ->leftjoin('sp_shop_user_2 as s', 'o.seller_id', 's.member_id')
            ->where('o.id', $request['order_id'])
            ->select('m.mobile', 'm.cert_name', 's.mechanism', 'm.cert_no', 'o.mini_id', 'o.trade_no', 'o.realname', 'o.address',
                'o.month_total', 'o.address', 'o.address', 'o.id', 's.member_id', 'o.uid', 'o.seller_id', 'og.goods_title', 'og.imgurl', 'm.gender', 'og.setmeal_title')
            ->first()->toArray();
        if (empty($order['cert_no'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '用户身份证未上传');
        }
        $miniId = $order['mini_id'];
        $order['rent'] = OrderLease::where('order_id', $order['id'])->where('which_period', '1')->value('rent');
        $res = AlipayService::rentriskQuery($miniId, $order);

        if (!$res['status']) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '查询失败');
        }
        $res = $res['data'];
        MemberAlipayRisk::insertMember($order['cert_no'], $order['cert_name'], $order['mobile'], $order['uid'], $res['risk_name'], $res['risk_desc'], json_encode($res, JSON_UNESCAPED_UNICODE));
        //添加备注
        $add = [
            'uid' => $this->users['id'],
            'username' => $this->users['username'],
            'create_time' => time(),
            'remark' => $this->users['username'] . '查询支付宝小程序云智能租赁风控',
            'order_id' => $request['order_id']
        ];
        OrderRemark::insert($add);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功');
    }

    /**
     * 设置下一个订单
     * @param Request $request
     * @return false|string
     */
    public function setNextOrder(Request $request){
        $orderId = $request->input('orderId','');
        if(!$orderId){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        Order::where('trade_no',$orderId)->update(['username'=>$this->users['username']]);

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
    }
    /**
     * 订单加入待处理
     * @param Request $request
     * @return false|string
     */
    public function handleOrder(Request $request){
        $orderId = $request->input('orderId','');
        if(!$orderId){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        Order::where('id',$orderId)->update(['is_handle'=>'1']);

        OrderLog::addOrderLog($orderId,$this->users['id'],OrderLog::ORDER_LOG_OPERATE_TYPE_ADMIN,'订单待处理',OrderLog::ORDER_LOG_PROCESS);

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
    }

    //采购商品列表
    public function procureList(Request $request)
    {

        $data = RadarService::procureList($request);

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功',$data);
    }

    //采购商品列表需要数据
    public function procureData(Request $request)
    {
        $data = RadarService::procureData($request);

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功',$data);
    }


    //外部请求采购商品列表
    public function sendProcureList(Request $request)
    {

        $data = RadarService::sendProcureList($request);

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功',$data);
    }

    //采购单数据
    public function procureApplyData(Request $request)
    {
        $data = RadarService::procureApplyData($request);

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功',$data);
    }

    //外部申请采购商品  1
    public function applyProcure(Request $request)
    {

        $orderId = $request->input('orderId','');

        // $data = RadarService::getProcureMoney($request);
        // if($data){
            RadarService::applyProcure($request);

            Order::where('id',$orderId)->update(['is_apply_goods'=>'2']);

            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '申请成功');
        // }else{
        //     return ReturnMsg(CommonService::HTTP_CODE_FAIL, '采购账户余额不足，请充值');
        // }

    }

    //外部请求采购商品撤回  7
    public function sendProcureCancel(Request $request)
    {
        $trade_no = $request->input('trade_no','');
        Order::where('trade_no',$trade_no)->update(['is_apply_goods'=>'1']);
        $data = RadarService::sendProcureCancel($request);
        if($data){

            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '撤回成功');
        }else{

            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '采购单必须未受理状态');
        }
    }


    /**
     * 支付宝履约等级
     * @param Request $request
     * @return false|string
     */
    public function alipayPerformanceRead(Request $request){
        $order = Order::from('sp_order_2 as o')->where('o.id',$request['order_id'])
            ->leftjoin('sp_member_2 as m','m.id','o.uid')
            ->select(
                'o.id','o.uid','m.mobile','o.trade_no'
                ,'m.cert_name','m.cert_no','m.user_id','o.seller_id'
                ,'m.education' ,'o.status','m.level_performance'
            )
            ->first();


        if(!empty($order['level_performance'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'已查询');
        }

        $data=AlipayRiskService::levelPerformance($order['cert_no'],$order['uid'],$order['seller_id']);

        if($data=='未知'){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'查询失败');
        }

        //添加备注
        $add = [
            'uid'=>$this->users['id'],
            'username'=>$this->users['username'],
            'create_time'=>time(),
            'remark'=>$this->users['username'].'查看支付宝履约等级',
            'order_id'=>$request['order_id']
        ];
        OrderRemark::insert($add);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功');
    }

    /**
     * 支付宝风险识别
     * @param Request $request
     * @return false|string
     */
    public function alipayRead(Request $request){
        $order = Order::from('sp_order_2 as o')->where('o.id',$request['order_id'])
            ->leftjoin('sp_member_2 as m','m.id','o.uid')
            ->select(
                'o.id','o.uid','m.mobile','o.trade_no','o.seller_id'
                ,'m.cert_name','m.cert_no','m.user_id'
                ,'m.education' ,'o.status','m.level'
            )
            ->first();

        if(empty($order['user_id'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'非支付宝用户');
        }

        if(!empty($order['level'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'已查询');
        }

        $data=SafeService::alipayRead($order['user_id'],$order['seller_id']);

        if($data=='未知'){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'查询失败');
        }


        //添加备注
        $add = [
            'uid'=>$this->users['id'],
            'username'=>$this->users['username'],
            'create_time'=>time(),
            'remark'=>$this->users['username'].'查看支付宝风险评估等级',
            'order_id'=>$request['order_id']
        ];
        OrderRemark::insert($add);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功');
    }



    /**
     * 获取公证链接
     */
    public function getNotaryUrl(Request $request){
        $order_id = $request->input('orderId');
        if(!empty($order_id)){
            $order= Order::where('id',$order_id)->select('contract_id','is_notary','notary_url','pact_four_url')->first();
            $contract_id=$order['contract_id'];
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '订单不存在');
        }
        if(!$order['pact_four_url']){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '未签署四方合同');
        }
        $is_notary=$order['is_notary'];;
        if($is_notary){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'已协议公证成功');
        }

        if($contract_id){
            $res=NotarizeService::notaryQuery($contract_id);
            if($res['state']=='3'){
                $data=$res['data'];
                foreach ($data as $k=>$v){
                    $fileUrl=$v['fileURL'];
                    $fileName = rand(10,99).$k.$contract_id.".pdf";
                    $file = file_get_contents($fileUrl);
                    $path = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/storage/tmp/'.$fileName;

                    file_put_contents($path,$file);
                    $uploud_res = AlipayService::upload($fileName,$path);
                    if($uploud_res['status']){
                        if(file_exists($path)){
                            unlink($path);
                        }
                    }
                    $data[$k]['dlzpdfurl']=$fileName;
                }
                Order::where('contract_id',$contract_id)->limit(1)->update(['is_notary'=>1,'notary_info'=>json_encode($data)]);
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, $res['msg'],$res);
            }
        }

        $res=NotarizeService::notaryReady($contract_id);
        if($res['status']==204){
            $res=NotarizeService::get_notary_link($contract_id);
        }

        if($res['status']!=200||!$contract_id){
            $order = Order::from('sp_order_2 as o')->where('o.id',$order_id)
                ->leftjoin('sp_order_credit as c','c.order_id','o.id')
                ->leftjoin('sp_member_2 as u','o.uid','u.id')
                ->leftjoin('sp_shop_user_2 as su','o.seller_id','su.member_id')
                ->leftjoin('sp_order_goods_2 as og','o.id','og.order_id')
                ->leftjoin('sp_goods_2 as g','og.goods_id','g.id')
                ->select(
                    'o.id','o.trade_no','o.begin_time','o.zulinbao_order_id','o.end_time','o.pact_url_3','o.uid','o.seller_id','o.is_insurance_choose',
                    'o.flow_id','o.mobile','o.user_email','o.sign_num','o.address','o.rent_total','o.month_total','o.amount','o.realname','o.rent_total',
                    'o.foregift','o.status','o.prov','o.city','o.area','o.sign_status','o.is_rent_choose'
                    ,'c.platformLoanNo','o.create_time'
                    ,'u.ant_user_id','u.mobile as user_mobile','u.id as x_user_id','u.cert_name','u.user_id','u.cert_no','u.esign_user_id'
                    ,'u.idcard_front','u.idcard_back','og.id as order_goods_id ','og.pact_type','og.is_buy_out','og.goods_title','og.setmeal_title',
                    'og.lease_set','og.setmeal_price','og.setmeal_foregift','o.prepayment_total','o.pact_url',
                    'og.goods_id','og.setmeal_id','og.id as order_goods_id','og.spec_id','og.lease_price','og.lease_time','og.website_price','og.start_date'
                    ,'su.mechanism','su.faname','su.shopname','su.contact_tel','su.company','su.company_area','su.companyid','su.id as shop_user_id'
                    ,'su.ant_user_id as mer_ant_id','o.sign_no','su.esign_seal_id','su.ant_seal_id'
                    ,'g.new_grade')
                ->first();
            $order = $order ? $order->toArray() : [];
            NotarizeService::uploadOrderInfo($order);
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, $res['msg']);
        }

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$res);

    }


    /**
     * 公证合同申请
     */
    public function getNotaryContract(Request $request){
        $order_id = $request->input('orderId');

        $order = Order::from('sp_order_2 as o')->where('o.id',$order_id)
            ->leftjoin('sp_order_credit as c','c.order_id','o.id')
            ->leftjoin('sp_member_2 as u','o.uid','u.id')
            ->leftjoin('sp_shop_user_2 as su','o.seller_id','su.member_id')
            ->leftjoin('sp_order_goods_2 as og','o.id','og.order_id')
            ->leftjoin('sp_goods_2 as g','og.goods_id','g.id')
            ->select(
                'o.id','o.trade_no','o.begin_time','o.zulinbao_order_id','o.end_time','o.pact_url_3','o.uid','o.seller_id','o.is_insurance_choose',
                'o.flow_id','o.mobile','o.user_email','o.sign_num','o.address','o.rent_total','o.month_total','o.amount','o.realname','o.rent_total',
                'o.foregift','o.status','o.prov','o.city','o.area','o.sign_status','o.is_rent_choose'
                ,'c.platformLoanNo','o.create_time'
                ,'u.ant_user_id','u.mobile as user_mobile','u.id as x_user_id','u.cert_name','u.user_id','u.cert_no','u.esign_user_id'
                ,'u.idcard_front','u.idcard_back','og.id as order_goods_id ','og.pact_type','og.is_buy_out','og.goods_title','og.setmeal_title',
                'og.lease_set','og.setmeal_price','og.setmeal_foregift','o.prepayment_total','o.pact_url',
                'og.goods_id','og.setmeal_id','og.id as order_goods_id','og.spec_id','og.lease_price','og.lease_time','og.website_price','og.start_date'
                ,'su.mechanism','su.faname','su.shopname','su.contact_tel','su.company','su.company_area','su.companyid','su.id as shop_user_id'
                ,'su.ant_user_id as mer_ant_id','o.sign_no','su.esign_seal_id','su.ant_seal_id','four_sign_flow_id','o.pact_four_sign_url','o.pact_four_sign_url'
                ,'o.four_sign_status'
                ,'g.new_grade')
            ->first();
        if(empty($order)){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '订单不存在');
        }
        $signStatus = EsignService::signStatus($order['four_sign_flow_id']);
        if(isset($signStatus['pact_four_sign_url']) && $signStatus['pact_four_sign_url']){
            $data['four_sign_flow_id'] = $order['four_sign_flow_id'];
            $data['pact_four_sign_url'] = $order['pact_four_sign_url'];
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'合同签署中，请勿重复发起',$data);
        }
        $signStatus = EsignService::signStatus($order['four_sign_flow_id']);
        if(isset($signStatus['flowStatus']) && $signStatus['flowStatus'] == EsignService::ESIGN_STATUS_DRAFT){
            $data['flowId'] = $order['four_sign_flow_id'];
            $data['signerId'] = $order['esign_user_id'];
            $data['signUrl'] = $order['pact_four_sign_url'];
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'草稿！重新发起签署',$data);
        }
        if(isset($signStatus['flowStatus']) && $signStatus['flowStatus']==EsignService::ESIGN_STATUS_SIGNING){
            $data['flowId'] = $order['four_sign_flow_id'];
            $data['signerId'] = $order['esign_user_id'];
            $data['signUrl'] = $order['pact_four_sign_url'];
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'合同签署中，请勿重复发起',$data);
        }
        if(isset($signStatus['flowStatus']) && $signStatus['flowStatus'] == EsignService::ESIGN_STATUS_FINISH){
            Order::where('id',$order_id)->update(['four_sign_status'=>EsignService::ESIGN_STATUS_FINISH]);
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '合同签署已完成');
        }else{
            Order::where('id',$order_id)->update(['four_sign_status'=>EsignService::ESIGN_STATUS_SIGNING]);
        }

        if(empty($order['esign_user_id'])){
            $result = EsignService::addPersonAccountID($order['cert_no'],$order['cert_name'],$order['cert_no'],$order['user_mobile']);
            $data1 = $result['data'];
            if(!empty($data1['accountId'])){
                $esign_user_id = $data1['accountId'];
                MemberOther::where('id',$order['uid'])->update(['esign_user_id'=>$esign_user_id]);
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '合同签署账号创建失败');
            }
        }else{
            $user_mobile = !empty($order['user_mobile'])?$order['user_mobile']:$order['mobile'];
            $esign_user_id = EsignService::updateAccounts($order['esign_user_id'],$user_mobile,$order['cert_name']);
            if(empty($esign_user_id)){
                $result = EsignService::addPersonAccountID($order['cert_no'],$order['cert_name'],$order['cert_no'],$order['user_mobile']);
                // dd(1);
                $data1 = $result['data'];
                if(!empty($data1['accountId'])){
                    $esign_user_id = $data1['accountId'];
                    MemberOther::where(['id'=>$this->member['id']])->update(['esign_user_id'=>$esign_user_id]);
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '合同签署账号创建失败');
                }
            }
        }
        $shop_user = ShopUser::where('member_id',$order['seller_id'])->select('mechanism','company','faname','esign_shop_id','company_area','agreement','contact_tel','esign_seal_id','companyid')->first();
        $shop_user = $shop_user ? $shop_user->toArray() : [];

        //创建合同
        $result = EsignService::createFlowOneStepShopSupplierFour($order,$shop_user,$esign_user_id);
        if(isset($result['data']) && !empty($result['data']['flowId'])){
            $flowId = $result['data']['flowId'];
            $url=EsignService::executeUrl($flowId,$esign_user_id);
            $res = Order::where('id',$order_id)->update(['four_sign_flow_id'=>$flowId,
                'four_sign_status'=>EsignService::ESIGN_STATUS_SIGNING,'pact_four_sign_url'=>$url]);
            if($res){
                $data['flowId'] = $flowId;
                $data['signerId'] = $esign_user_id;
                $data['signUrl'] = $url;
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'合同发起成功',$data);
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '合同签署信息记录失败');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '合同创建失败，请联系客服');
        }

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$result);

    }

    /**
     * 流水账单报告
     * @param Request $request
     * @return void
     */
    public function billReport(Request $request){
        $order = Order::from('sp_order_2 as o')->where('o.id',$request['order_id'])
            ->leftjoin('sp_member_2 as m','m.id','o.uid')
            ->select(
                'o.id','o.uid','m.mobile','o.trade_no','o.seller_id'
                ,'m.cert_name','m.cert_no','m.user_id'
                ,'m.education' ,'o.status','m.level'
            )
            ->first();
        if(empty($order)){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'订单不存在');
        }
        $res=BillReport::query($order['cert_name'],$order['cert_no'],$order['mobile']);
        if($res){
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$res);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'流水报告获取失败');
        }


    }

    /**
     * qnyp风控查询
     * @param Request $request
     * @return false|string
     */
    public function accurateRiskQuery(Request $request)
    {

        throw new ApiErrorException('出错了', '暂不支持风控调用');

//        $order = Order::from('sp_order_2 as o')->where('o.id',$request['order_id'])
//            ->select(
//                'o.id','o.uid','o.trade_no'
//            )
//            ->first();
//        $one=MemberQnRisk::query($order['uid']);
//        if(!empty($one)){
//            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'已查询');
//        }
//
//        $order = Order::from('sp_order_2 as o')->where('o.id',$request['order_id'])
//            ->leftjoin('sp_member_2 as m','m.id','o.uid')
//            ->select(
//                'o.id','o.uid','o.mobile as user_mobile','o.pact_url_5','o.trade_no'
//                ,'m.cert_name','m.cert_no'
//                ,'m.mobile','m.esign_user_id','m.esign_seal_id','m.user_id','m.idcard_front','m.idcard_back'
//            )
//            ->first();
//
//        if(empty($order['cert_no'])){
//            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'用户身份证未上传');
//        }
//        $res=RiskQnypService::query($order);
//
//        if(!$res){
//            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'授权书生成失败');
//        }
//        $res=$res['data'];
//        MemberQnRisk::insertMember($order['cert_no'],$order['cert_name'],$order['uid'],$res['result'],json_encode($res,JSON_UNESCAPED_UNICODE));
//        //添加备注
//        $add = [
//            'uid'=>$this->users['id'],
//            'username'=>$this->users['username'],
//            'create_time'=>time(),
//            'remark'=>$this->users['username'].'查询风控建议',
//            'order_id'=>$request['order_id']
//        ];
//        OrderRemark::insert($add);
//        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功');
    }


}
